在C语言中,运算符是一个符号,告诉编译器执行特定的数学或逻辑函数,C语言提供丰富的内置运算符,并提供以下类型的运算符
-
- 算术运算符
- 关系运算符
- 逻辑运算符
- 按位运算符
- 赋值运算符
- 其它运算符
在本章中,我们将学习每个运算符的工作方式。打开Visual Studio 2017创建一个Win32
Console Application 项目,名称为:c-operators 。
1.算术运算符
下表显示了C语言支持的所有算术运算符。假设变量A的值是10,变量B的值是20,那么
-
运算符 |
描述 |
示例 |
+ |
将两个操作数相加 |
A + B = 30 |
- |
从第一个操作数减去第二个操作数 |
A - B
= -10 |
* |
将两个操作数相乘 |
A * B
= 200 |
/ |
将第一个操作数除以第二个操作数 |
|
% |
模数运算符和整数除法后的余数。 |
B % A
= 0 |
++ |
递增运算符将整数值增加1。 |
B % A
= 0 |
-- |
递减运算符将整数值减1。 |
A-- =
9 |
创建一个源代码文件:arithmetic_operators.c,如下代码
-
#include
<stdio.h>
void 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
请按任意键继续. . .
|
2.关系运算符
下表显示了C语言支持的关系运算符。假设变量A=10,变量B=20,则
-
运算符 |
描述 |
示例 |
== |
检查两个操作数的值是否相等。 如果相等,则条件成立。 |
(A == B)结果为false |
!= |
检查两个操作数的值是否相等。
如果值不相等,则条件成立。 |
(A !=
B) 结果为true |
> |
检查左操作数的值是否大于右操作数的值。
如果是,则条件成立。 |
(A >
B) 结果为false |
< |
检查左操作数的值是否小于右操作数的值。
如果是,则条件成立。 |
(A <
B)结果为true |
>= |
检查左操作数的值是否大于等于右操作数的值。
如果是,则条件成立。 |
(A >=
B) 结果为false |
<= |
检查左操作数的值是否小于等于右操作数的值。
如果是,则条件成立。 |
(A <=
B)结果为true |
创建一个源代码文件:relational_operators.c,如下代码
-
#include
<stdio.h>
main() {
int a = 21;
int b = 10;
int c ;
if( a == b ) {
printf("Line 1 - a is equal to b\n"
);
}
else {
printf("Line 1 - a is not equal to b\n"
);
}
if ( a < b ) {
printf("Line 2 - a is less than b\n"
);
}
else {
printf("Line 2 - a is not less than b\n"
);
}
if ( a > b ) {
printf("Line 3 - a is greater than b\n"
);
}
else {
printf("Line 3 - a is not greater than
b\n" );
}
/* Lets change value of a and b */
a = 5;
b = 20;
if ( a <= b ) {
printf("Line 4 - a is either less than
or equal to b\n" );
}
if ( b >= a ) {
printf("Line 5 - b is either greater
than or equal to b\n" );
}
}
|
执行上面示例代码,得到以下结果 -
Line 1
- a is not equal to b
Line 2 - a is not less than b
Line 3 - a is greater than b
Line 4 - a is either less than or equal to
b
Line 5 - b is either greater than or equal
to b
请按任意键继续. . .
|
3.逻辑运算符
下表显示了C语言支持的所有逻辑运算符。 假设变量A=1,变量B=0,则
-
运算符 |
描述 |
示例 |
&& |
逻辑与运算符。 如果两个操作数都不为零,则条件成立。 |
(A && B)结果为false |
|| |
称为逻辑或运算符。如果两个操作数中的任何一个非零,则条件成立。 |
(A|| B)结果为true |
! |
称为逻辑非运算符,它用于反转其操作数的逻辑状态。如果条件为真,则逻辑NOT运算符将使其结果为false。
|
|
示例:创建一个源文件:logical_operators.c,代码如下
-
#include
<stdio.h>
main() {
int a = 5;
int b = 20;
int c ;
if ( a && b ) {
printf("Line 1 - Condition is true\n"
);
}
if ( a || b ) {
printf("Line 2 - Condition is true\n"
);
}
/* lets change the value of a and b */
a = 0;
b = 10;
if ( a && b ) {
printf("Line 3 - Condition is true\n"
);
}
else {
printf("Line 3 - Condition is not true\n"
);
}
if ( !(a && b) ) {
printf("Line 4 - Condition is true\n"
);
}
} |
执行上面代码,得到以下结果 -
Line 1
- Condition is true
Line 2 - Condition is true
Line 3 - Condition is not true
Line 4 - Condition is true |
4.按位运算符
按位运算符对位进行操作,并执行逐位运算。 &,|和^的真值表如下
-
p |
q |
p & q |
p/q |
p ^ q |
0 |
0 |
0 |
0 |
0 |
0 |
1 |
0 |
1 |
1 |
1 |
1 |
1 |
1 |
0 |
1 |
0 |
0 |
1 |
1 |
假设A = 60,B = 13,二进制格式如下:
A = 0011
1100
B = 0000 1101
-----------------
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
~A = 1100 0011 |
下表列出了C语言支持的按位运算符。假设变量A=60,变量B=13,则
-
运算符 |
描述 |
示例 |
& |
如果二进制AND运算符存在于两个操作数中,则二进制AND运算符将对结果复制一位。 |
(A&B)= 12,即0000 1100 |
| |
二进制OR运算符如果存在于任一操作数中,则复制一位。 |
(A||B)
= 61 即 0011 1101
|
^ |
二进制XOR操作符复制该位,如果它设置在一个操作数中,而不是两者。 |
(A ^ B)
= 49, 即, 0011 0001 |
~ |
二进制补码运算符是一元的,具有“翻转”位的作用。 |
(~A)= -61,即
1100 0011的补码形式。 |
<< |
二进制左移操作符,左操作数值左移由右操作数指定的位数。 |
A <<
2 = 240 即, 1111 0000 |
>> |
二进制右移操作符,左操作数值被右操作数指定的位移动。 |
A >>
2 = 15 即,0000 1111 |
示例: 创建一个源代码文件:bitwise_operators.c,代码如下所示
-
#include
<stdio.h>
main() {
unsigned int a = 60; /* 60 = 0011 1100 */
unsigned int b = 13; /* 13 = 0000 1101 */
int c = 0;
c = a & b; /* 12 = 0000 1100 */
printf("Line 1 - Value of c is %d\n",
c );
c = a | b; /* 61 = 0011 1101 */
printf("Line 2 - Value of c is %d\n",
c );
c = a ^ b; /* 49 = 0011 0001 */
printf("Line 3 - Value of c is %d\n",
c );
c = ~a; /*-61 = 1100 0011 */
printf("Line 4 - Value of c is %d\n",
c );
c = a << 2; /* 240 = 1111 0000 */
printf("Line 5 - Value of c is %d\n",
c );
c = a >> 2; /* 15 = 0000 1111 */
printf("Line 6 - Value of c is %d\n",
c );
}
|
执行上面代码,得到以下结果 -
Line 1
- Value of c is 12
Line 2 - Value of c is 61
Line 3 - Value of c is 49
Line 4 - Value of c is -61
Line 5 - Value of c is 240
Line 6 - Value of c is 15
请按任意键继续. . .
|
5.赋值运算符
下表列出了C语言支持的赋值运算符 -
运算符 |
描述 |
示例 |
= |
简单赋值运算符,将右侧操作数的值分配给左侧操作数 |
C = A + B,将A + B的值分配给C |
+= |
相加与赋值运算符。它将右操作数添加到左操作数,并将结果分配给左操作数。 |
C + =
A等价于C = C + A
|
-= |
相减与赋值运算符。它从左操作数中减去右操作数,并将结果分配给左操作数。 |
C -= A等价于
C = C - A |
*= |
乘以与赋值运算符。它将右操作数与左操作数相乘,并将结果分配给左操作数。 |
C * = A等价于C
= C * A |
/= |
除以与赋值运算符。它将左操作数与右操作数分开,并将结果分配给左操作数。 |
C /= A等价于C
= C / A |
%= |
模数与赋值运算符。它需要使用两个操作数的模数,并将结果分配给左操作数。 |
C %= A等价于C = C % A |
<<= |
左移与赋值运算符 |
C <<=
2等价于C = C << 2 |
>>= |
右移与赋值运算符 |
C >>
= 2等价于C = C >> 2 |
&= |
按位与赋值运算符 |
C &= 2等价于C = C & 2 |
^= |
按位异或运算符和赋值运算符。 |
C ^= 2等价于C
= C ^ 2 |
|= |
按位包含OR和赋值运算符。 |
c |= 等价于
c=c | 2 |
示例: 创建一个源文件:assignment_operators.c
,其代码如下 -
#include
<stdio.h>
void main() {
int a = 21;
int c ;
c = a;
printf("Line 1 - = Operator Example,
Value of c = %d\n", c );
c += a;
printf("Line 2 - += Operator Example,
Value of c = %d\n", c );
c -= a;
printf("Line 3 - -= Operator Example,
Value of c = %d\n", c );
c *= a;
printf("Line 4 - *= Operator Example,
Value of c = %d\n", c );
c /= a;
printf("Line 5 - /= Operator Example,
Value of c = %d\n", c );
c = 200;
c %= a;
printf("Line 6 - %= Operator Example,
Value of c = %d\n", c );
c <<= 2;
printf("Line 7 - <<= Operator Example,
Value of c = %d\n", c );
c >>= 2;
printf("Line 8 - >>= Operator Example,
Value of c = %d\n", c );
c &= 2;
printf("Line 9 - &= Operator Example,
Value of c = %d\n", c );
c ^= 2;
printf("Line 10 - ^= Operator Example,
Value of c = %d\n", c );
c |= 2;
printf("Line 11 - |= Operator Example,
Value of c = %d\n", c );
}
|
执行上面代码,得到以下结果 -
Line 1
- = Operator Example, Value of c = 21
Line 2 - += Operator Example, Value of c =
42
Line 3 - -= Operator Example, Value of c =
21
Line 4 - *= Operator Example, Value of c =
441
Line 5 - /= Operator Example, Value of c =
21
Line 6 - = Operator Example, Value of c =
11
Line 7 - <<= Operator Example, Value
of c = 44
Line 8 - >>= Operator Example, Value
of c = 11
Line 9 - &= Operator Example, Value of
c = 2
Line 10 - ^= Operator Example, Value of c
= 0
Line 11 - |= Operator Example, Value of c
= 2
请按任意键继续. . |
6.其他操作符:
sizeof和三元运算符除了上面讨论的运算符,还有一些其他重要的运算符,包括sizeof和?
:也被C语言所支持。
运算符 |
描述 |
示例 |
sizeof() |
返回变量的大小 |
sizeof(a),其中a为整数,将返回4。 |
& |
返回变量的地址 |
&a;
返回变量的实际地址。 |
* |
指向变量的指针 |
*a; |
? : |
条件表达式 |
如果条件是真的?
那么返回值X:否则返回Y |
示例: 创建一个源文件:sizeof_operator.c
,其代码如下 -
#include
<stdio.h>
void main() {
int a = 4;
short b;
double c;
int* ptr;
/* example of sizeof operator */
printf("Line 1 - Size of variable a =
%d\n", sizeof(a));
printf("Line 2 - Size of variable b =
%d\n", sizeof(b));
printf("Line 3 - Size of variable c=
%d\n", sizeof(c));
/* example of & and * operators */
ptr = &a; /* 'ptr' now contains the address
of 'a'*/
printf("value of a is %d\n", a);
printf("*ptr is %d.\n", *ptr);
/* example of ternary operator */
a = 10;
b = (a == 1) ? 20 : 30;
printf("Value of b is %d\n", b);
b = (a == 10) ? 20 : 30;
printf("Value of b is %d\n", b);
}
|
执行上面代码,得到以下结果 -
Line 1
- Size of variable a = 4
Line 2 - Size of variable b = 2
Line 3 - Size of variable c= 8
value of a is 4
*ptr is 4.
Value of b is 30
Value of b is 20
请按任意键继续. . .
|
7.运算符优先级
运算符优先级决定表达式中术语的分组,并决定如何评估计算表达式。
某些运算符的优先级高于其他运营商; 例如,乘法运算符的优先级高于加法运算符,则先要执行乘法运算符的运算。
让我们通过下面的例子了解优先级:
int value
= 10 + 20 * 10;
|
value变量计算结果为:210,因为*(乘法运算符)的优先级比+(加法运算符)高,所以在+(加法运算符)之前进行求值。
C语言运算符的优先级和关联性如下:
运算符 |
描述 |
示例 |
后缀 |
() [] -> . ++ - - |
左到右 |
一元 |
+ - !
~ ++ - - (type)* & sizeof |
右到左 |
乘法 |
* / % |
左到右 |
加法 |
+ - |
左到右 |
位移 |
<<
>> |
左到右 |
关系 |
< <=
> >= |
左到右 |
等于 |
== != |
左到右 |
按位与 |
& |
左到右 |
位异或 |
^ |
左到右 |
按位或 |
/ |
左到右 |
逻辑与 |
&& |
左到右 |
逻辑或 |
// |
左到右 |
条件 |
?: |
右到左 |
赋值 |
= += -=
*= /= %=>>= <<= &= ^= /= |
右到左 |
逗号 |
, |
左到右 |
示例: 创建一个源文件:operators_precedence.c
,其代码如下 -
#include
<stdio.h>
void main() {
int a = 20;
int b = 10;
int c = 15;
int d = 5;
int e;
e = (a + b) * c / d; // ( 30 * 15 ) / 5
printf("Value of (a + b) * c / d is :
%d\n", e);
e = ((a + b) * c) / d; // (30 * 15 ) / 5
printf("Value of ((a + b) * c) / d is
: %d\n", e);
e = (a + b) * (c / d); // (30) * (15/5)
printf("Value of (a + b) * (c / d) is
: %d\n", e);
e = a + (b * c) / d; // 20 + (150/5)
printf("Value of a + (b * c) / d is :
%d\n", e);
return 0;
}
|
执行上面代码,得到以下结果 -
Value
of (a + b) * c / d is : 90
Value of ((a + b) * c) / d is : 90
Value of (a + b) * (c / d) is : 90
Value of a + (b * c) / d is : 50
请按任意键继续. . .
|
|