Show 例子 1
优质
小牛编辑
138浏览
2023-12-01
下表显示了C语言支持的所有算术运算符。 假设变量A保持10,变量B保持20,则 -
操作者 | 描述 | 例 |
---|---|---|
+ | 添加两个操作数。 | A + B = 30 |
− | 从第一个减去第二个操作数。 | A - B = -10 |
* | 将两个操作数相乘。 | A * B = 200 |
/ | Divides numerator by de-numerator. | B/A = 2 |
% | 模数运算符和整数除法后的余数。 | B%A = 0 |
++ | 递增运算符将整数值增加1。 | A ++ = 11 |
-- | 递减运算符将整数值减1。 | A-- = 9 |
例子 (Example)
尝试以下示例来了解C中可用的所有算术运算符 -
#include <stdio.h>
main() {
int a = 21;
int b = 10;
int c ;
c = a + b;
printf("Line 1 - Value of c is %d\n", c );
c = a - b;
printf("Line 2 - Value of c is %d\n", c );
c = a * b;
printf("Line 3 - Value of c is %d\n", c );
c = a/b;
printf("Line 4 - Value of c is %d\n", c );
c = a % b;
printf("Line 5 - Value of c is %d\n", c );
c = a++;
printf("Line 6 - Value of c is %d\n", c );
c = a--;
printf("Line 7 - Value of c is %d\n", 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