位置:首页 » 文章/教程分享 » 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