基本算术运算包括加法,减法,乘法和减法。它们一般对数字数据类型执行基本算术运算,如:int
,float
和double
。
加法操作
下面给出的示例代码解释了C语言中的加法运算符:
#include <stdio.h>
int main() {
int op1, op2, sum; // variable declaration
op1 = 5; // variable definition
op2 = 3;
sum = op1 + op2; // addition operation
printf("sum of %d and %d is %d", op1, op2, sum);
}
执行上面示例代码,得到以下结果 -
sum of 5 and 3 is 8
减法运算
C语言中的减法运算,如下面给出的示例代码 -
#include <stdio.h>
int main() {
int op1, op2, sub; // variable declaration
op1 = 5; // variable definition
op2 = 3;
sub = op1 - op2; // subtraction operation
printf("Output of %d − %d is %d", op1, op2, sub);
}
执行上面示例代码,得到以下结果 -
Output of 5 − 3 is 2
乘法运算
下面给出的示例代码说明了C语言中的乘法。星号(*
)用作乘法运算符。
#include <stdio.h>
int main() {
int op1, op2, mul; // variable declaration
op1 = 5; // variable definition
op2 = 3;
mul = op1 * op2; // multiplication operation
printf("Output of %d multiplied by %d is %d", op1, op2, mul);
}
执行上面示例代码,得到以下结果 -
Output of 5 multiplied by 3 is 15
除法运算
下面给出的示例代码说明了C语言中的除法运算。如下代码所示 -
#include <stdio.h>
int main() {
int op1, op2, div; // variable declaration
op1 = 6; // variable definition
op2 = 3;
div = 6 / 3; // division operation
printf("Output of %d divide by %d is %d", op1, op2, div);
}
执行上面示例代码,得到以下结果 -
Output of 6 divide by 3 is 2
上一篇:
C语言变量示例程序
下一篇:
C语言比较三个整数示例