C語言算術運算符
算術運算符
下表列出了所有C語言支持的算術運算符。假設變量A=10和變量B=20則:
運算符 | 描述 | 示例 |
---|---|---|
+ | 兩個操作數相加 | A + B = 30 |
- | 第一操作數減去第二個操作數 | A - B = -10 |
* | 兩個操作數相乘 | A * B = 200 |
/ | 分子除以分母 | B / A = 2 |
% | 模運算和整數除法後的餘數 | B % A = 0 |
++ | 遞增操作者增加一個整數值 | A++ = 11 |
-- | 遞減操作減少一個整數值 | A-- = 9 |
例子
試試下麵的例子就明白了所有的C編程語言提供的算術運算符:
#include <stdio.h> main() { int a = 21; int b = 10; int c ; c = a + b; printf("Line 1 - Value of c is %d ", c ); c = a - b; printf("Line 2 - Value of c is %d ", c ); c = a * b; printf("Line 3 - Value of c is %d ", c ); c = a / b; printf("Line 4 - Value of c is %d ", c ); c = a % b; printf("Line 5 - Value of c is %d ", c ); c = a++; printf("Line 6 - Value of c is %d ", c ); c = a--; printf("Line 7 - Value of c is %d ", c ); }
當編譯和執行上麵的程序,它會產生以下結果:
Line 1 - Value of c is 31 Line 2 - Value of c is 11 Line 3 - Value of c is 210 Line 4 - Value of c is 2 Line 5 - Value of c is 1 Line 6 - Value of c is 21 Line 7 - Value of c is 22