# C语言中的运算符|S1（算术运算符）

2021年4月25日17:31:22 发表评论 1,080 次浏览

## 本文概述

• 算术运算符s(+, -, *, /, %, 后递增, 前递增, 后递减, 前递减)
• 关系运算符(==, ！=, >, <, > =＆<=)逻辑运算符(&&, ||和!)
• 按位运算符(＆, |, ^, ~, >>和<<)
• 赋值运算符s(=, + =, -=, * =等)
• 其他运营商(条件, 逗号, sizeof, 地址, 重定向)

• 加成：的‘+’运算符将两个操作数相加。例如, x + y.
• 减法：的‘-’运算符减去两个操作数。例如, 设.
• 乘法：的‘*’运算符将两个操作数相乘。例如, x * y.
• 师：的‘/’运算符将第一个操作数除以第二个。例如, x / y.
• 模量：的‘%’当第一个操作数除以第二个操作数时, 运算符将返回余数。例如, x%y.

## C

``````//C program to demonstrate working of binary arithmetic operators
#include <stdio.h>

int main()
{
int a = 10, b = 4, res;

//printing a and b
printf ( "a is %d and b is %d\n" , a, b);

res = a + b; //addition
printf ( "a+b is %d\n" , res);

res = a - b; //subtraction
printf ( "a-b is %d\n" , res);

res = a * b; //multiplication
printf ( "a*b is %d\n" , res);

res = a /b; //division
printf ( "a/b is %d\n" , res);

res = a % b; //modulus
printf ( "a%b is %d\n" , res);

return 0;
}``````

## C ++

``````#include <iostream>
using namespace std;

int main() {
int a = 10, b = 4, res;

//printing a and b
cout<<"a is " <<a<<" and b is " <<b<<"\n" ;

res = a + b;
cout <<"a+b is: " <<res <<"\n" ;

//subtraction
res = a - b;
cout <<"a-b is: " <<res <<"\n" ;

//multiplication
res = a * b;
cout <<"a*b is: " <<res <<"\n" ;

//division
res = a /b;
cout <<"a/b is: " <<res <<"\n" ;

//modulus
res = a % b;
cout <<"a%b is: " <<res <<"\n" ;

return 0;
}``````

``````a is 10 and b is: 4
a+b is: 14
a-b is: 6
a*b is: 40
a/b is: 2
a%b is: 2``````

• 增量：的‘++’运算符用于递增整数值。当放置在变量名称(也称为预增量运算符)之前时, 其值会立即增加。例如, ++ x.
当将其放在变量名之后(也称为后递增运算符)时, 其值将暂时保留, 直到执行该语句为止, 并且在下一个语句执行之前对其进行更新。例如, x ++.
• 减量：的‘– –’运算符用于减少整数值。当放置在变量名称(也称为预减运算符)之前时, 其值将立即减小。例如, - - X.
当将其放在变量名(也称为后减运算符)之后时, 其值将暂时保留, 直到执行该语句为止, 并且在下一个语句执行之前对其进行更新。例如, X - -.

## C

``````//C program to demonstrate working of Unary arithmetic operators
#include <stdio.h>

int main()
{
int a = 10, b = 4, res;

//post-increment example:
//res is assigned 10 only, a is not updated yet
res = a++;
printf ( "a is %d and res is %d\n" , a, res); //a becomes 11 now

//post-decrement example:
//res is assigned 11 only, a is not updated yet
res = a--;
printf ( "a is %d and res is %d\n" , a, res); //a becomes 10 now

//pre-increment example:
//res is assigned 11 now since a is updated here itself
res = ++a;
//a and res have same values = 11
printf ( "a is %d and res is %d\n" , a, res);

//pre-decrement example:
//res is assigned 10 only since a is updated here itself
res = --a;
//a and res have same values = 10
printf ( "a is %d and res is %d\n" , a, res);

return 0;
}``````

## C ++

``````#include <iostream>
using namespace std;

int main() {
int a = 10, b = 4, res;

//post-increment example:
//res is assigned 10 only, a is not updated yet
res = a++;
//a becomes 11 now
cout <<"a is " <<a<<" and res is " <<res<<"\n" ;

//post-decrement example:
//res is assigned 11 only, a is not updated yet
res = a--;
//a becomes 10 now
cout <<"a is " <<a<<" and res is " <<res<<"\n" ;

//pre-increment example:
//res is assigned 11 now since a is updated here itself
res = ++a;
//a and res have same values = 11
cout <<"a is " <<a<<" and res is " <<res<<"\n" ;

//pre-decrement example:
//res is assigned 10 only since a is updated here itself
res = --a;
//a and res have same values = 10
cout <<"a is " <<a<<" and res is " <<res<<"\n" ;

return 0;
}``````

``````a is 11 and res is 10
a is 10 and res is 11
a is 11 and res is 11
a is 10 and res is 10``````

C语言中的运算符测验