在C語(yǔ)言中,運(yùn)算符是一個(gè)符號(hào),告訴編譯器執(zhí)行特定的數(shù)學(xué)或邏輯函數(shù),C語(yǔ)言提供豐富的內(nèi)置運(yùn)算符,并提供以下類型的運(yùn)算符 -
在本章中,我們將學(xué)習(xí)每個(gè)運(yùn)算符的工作方式。打開Visual Studio 2017創(chuàng)建一個(gè)Win32 Console Application 項(xiàng)目,名稱為:c-operators 。
下表顯示了C語(yǔ)言支持的所有算術(shù)運(yùn)算符。假設(shè)變量A的值是10,變量B的值是20,那么 -
| 運(yùn)算符 | 描述 | 示例 |
|---|---|---|
+ |
將兩個(gè)操作數(shù)相加 | A + B = 30 |
- |
從第一個(gè)操作數(shù)減去第二個(gè)操作數(shù) | A ? B = -10 |
* |
將兩個(gè)操作數(shù)相乘 | A * B = 200 |
/ |
將第一個(gè)操作數(shù)除以第二個(gè)操作數(shù) | |
% |
模數(shù)運(yùn)算符和整數(shù)除法后的余數(shù)。 | B % A = 0 |
++ |
遞增運(yùn)算符將整數(shù)值增加1。 |
A++ = 11 |
-- |
遞減運(yùn)算符將整數(shù)值減1。 | A-- = 9 |
創(chuàng)建一個(gè)源代碼文件: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 );
}
執(zhí)行上面示例代碼,得到以下結(jié)果 -
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
請(qǐng)按任意鍵繼續(xù). . .
下表顯示了C語(yǔ)言支持的關(guān)系運(yùn)算符。假設(shè)變量A=10,變量B=20,則 -
| 運(yùn)算符 | 描述 | 示例 |
|---|---|---|
== |
檢查兩個(gè)操作數(shù)的值是否相等。 如果相等,則條件成立。 | (A == B)結(jié)果為false |
!= |
檢查兩個(gè)操作數(shù)的值是否相等。 如果值不相等,則條件成立。 | (A != B) 結(jié)果為true |
> |
檢查左操作數(shù)的值是否大于右操作數(shù)的值。 如果是,則條件成立。 | (A > B) 結(jié)果為false |
< |
檢查左操作數(shù)的值是否小于右操作數(shù)的值。 如果是,則條件成立。 | (A < B)結(jié)果為true |
>= |
檢查左操作數(shù)的值是否大于等于右操作數(shù)的值。 如果是,則條件成立。 | (A >= B) 結(jié)果為false |
<= |
檢查左操作數(shù)的值是否小于等于右操作數(shù)的值。 如果是,則條件成立。 | (A <= B)結(jié)果為true |
創(chuàng)建一個(gè)源代碼文件: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" );
}
}
執(zhí)行上面示例代碼,得到以下結(jié)果 -
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
請(qǐng)按任意鍵繼續(xù). . .
下表顯示了C語(yǔ)言支持的所有邏輯運(yùn)算符。 假設(shè)變量A=1,變量B=0,則 -
| 運(yùn)算符 | 描述 | 示例 |
|---|---|---|
&& |
邏輯與運(yùn)算符。 如果兩個(gè)操作數(shù)都不為零,則條件成立。 | (A && B)結(jié)果為false |
![]() |
稱為邏輯或運(yùn)算符。如果兩個(gè)操作數(shù)中的任何一個(gè)非零,則條件成立。 | (A B)結(jié)果為true |
! |
稱為邏輯非運(yùn)算符,它用于反轉(zhuǎn)其操作數(shù)的邏輯狀態(tài)。如果條件為真,則邏輯NOT運(yùn)算符將使其結(jié)果為false。 |
示例:創(chuàng)建一個(gè)源文件: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" );
}
}
執(zhí)行上面代碼,得到以下結(jié)果 -
Line 1 - Condition is true
Line 2 - Condition is true
Line 3 - Condition is not true
Line 4 - Condition is true
按位運(yùn)算符對(duì)位進(jìn)行操作,并執(zhí)行逐位運(yùn)算。 &,|和^的真值表如下 -
| 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 |
假設(shè)A = 60,B = 13,二進(jìn)制格式如下:
A = 0011 1100
B = 0000 1101
-----------------
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
~A = 1100 0011
下表列出了C語(yǔ)言支持的按位運(yùn)算符。假設(shè)變量A=60,變量B=13,則 -
| 運(yùn)算符 | 描述 | 示例 |
|---|---|---|
& |
如果二進(jìn)制AND運(yùn)算符存在于兩個(gè)操作數(shù)中,則二進(jìn)制AND運(yùn)算符將對(duì)結(jié)果復(fù)制一位。 | (A&B)= 12,即0000 1100 |
![]() |
二進(jìn)制OR運(yùn)算符如果存在于任一操作數(shù)中,則復(fù)制一位。 | (A B) = 61, 即 0011 1101 |
^ |
二進(jìn)制XOR操作符復(fù)制該位,如果它設(shè)置在一個(gè)操作數(shù)中,而不是兩者。 | (A ^ B) = 49, 即, 0011 0001 |
~ |
二進(jìn)制補(bǔ)碼運(yùn)算符是一元的,具有“翻轉(zhuǎn)”位的作用。 | (~A)= -61,即 1100 0011的補(bǔ)碼形式。 |
<< |
二進(jìn)制左移操作符,左操作數(shù)值左移由右操作數(shù)指定的位數(shù)。 | A << 2 = 240 即, 1111 0000 |
>> |
二進(jìn)制右移操作符,左操作數(shù)值被右操作數(shù)指定的位移動(dòng)。 | A >> 2 = 15 即,0000 1111 |
示例: 創(chuàng)建一個(gè)源代碼文件: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 );
}
執(zhí)行上面代碼,得到以下結(jié)果 -
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
請(qǐng)按任意鍵繼續(xù). . .
下表列出了C語(yǔ)言支持的賦值運(yùn)算符 -
| 運(yùn)算符 | 描述 | 示例 |
|---|---|---|
= |
簡(jiǎn)單賦值運(yùn)算符,將右側(cè)操作數(shù)的值分配給左側(cè)操作數(shù) | C = A + B,將A + B的值分配給C |
+= |
相加與賦值運(yùn)算符。它將右操作數(shù)添加到左操作數(shù),并將結(jié)果分配給左操作數(shù)。 | C + = A等價(jià)于C = C + A |
-= |
相減與賦值運(yùn)算符。它從左操作數(shù)中減去右操作數(shù),并將結(jié)果分配給左操作數(shù)。 | C -= A等價(jià)于 C = C - A |
*= |
乘以與賦值運(yùn)算符。它將右操作數(shù)與左操作數(shù)相乘,并將結(jié)果分配給左操作數(shù)。 | C * = A等價(jià)于C = C * A |
/= |
除以與賦值運(yùn)算符。它將左操作數(shù)與右操作數(shù)分開,并將結(jié)果分配給左操作數(shù)。 | C /= A等價(jià)于C = C / A |
%= |
模數(shù)與賦值運(yùn)算符。它需要使用兩個(gè)操作數(shù)的模數(shù),并將結(jié)果分配給左操作數(shù)。 | C %= A等價(jià)于C = C % A |
<<= |
左移與賦值運(yùn)算符 | C <<= 2等價(jià)于C = C << 2 |
>>= |
右移與賦值運(yùn)算符 | C >> = 2等價(jià)于C = C >> 2 |
&= |
按位與賦值運(yùn)算符 | C &= 2等價(jià)于C = C & 2 |
^= |
按位異或運(yùn)算符和賦值運(yùn)算符。 | C ^= 2等價(jià)于C = C ^ 2 |
![]() |
按位包含OR和賦值運(yùn)算符。 | ![]() |
示例: 創(chuàng)建一個(gè)源文件: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 );
}
執(zhí)行上面代碼,得到以下結(jié)果 -
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
請(qǐng)按任意鍵繼續(xù). . .
除了上面討論的運(yùn)算符,還有一些其他重要的運(yùn)算符,包括sizeof和? :也被C語(yǔ)言所支持。
| 運(yùn)算符 | 描述 | 示例 |
|---|---|---|
sizeof() |
返回變量的大小 | sizeof(a),其中a為整數(shù),將返回4。 |
& |
返回變量的地址 | &a; 返回變量的實(shí)際地址。 |
* |
指向變量的指針 | *a; |
? : |
條件表達(dá)式 | 如果條件是真的? 那么返回值X:否則返回Y |
示例: 創(chuàng)建一個(gè)源文件: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);
}
執(zhí)行上面代碼,得到以下結(jié)果 -
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
請(qǐng)按任意鍵繼續(xù). . .
運(yùn)算符優(yōu)先級(jí)決定表達(dá)式中術(shù)語(yǔ)的分組,并決定如何評(píng)估計(jì)算表達(dá)式。 某些運(yùn)算符的優(yōu)先級(jí)高于其他運(yùn)營(yíng)商; 例如,乘法運(yùn)算符的優(yōu)先級(jí)高于加法運(yùn)算符,則先要執(zhí)行乘法運(yùn)算符的運(yùn)算。
讓我們通過(guò)下面的例子了解優(yōu)先級(jí):
int value = 10 + 20 * 10;
value變量計(jì)算結(jié)果為:210,因?yàn)?code>*(乘法運(yùn)算符)的優(yōu)先級(jí)比+(加法運(yùn)算符)高,所以在+(加法運(yùn)算符)之前進(jìn)行求值。
C語(yǔ)言運(yùn)算符的優(yōu)先級(jí)和關(guān)聯(lián)性如下:
| 分類 | 運(yùn)算符 | 關(guān)聯(lián)性 |
|---|---|---|
| 后綴 | () [] -> . ++ - - |
左到右 |
| 一元 | + - ! ~ ++ - - (type)* & sizeof |
右到左 |
| 乘法 | * / % |
左到右 |
| 加法 | + - |
左到右 |
| 位移 | << >> |
左到右 |
| 關(guān)系 | < <= > >= |
左到右 |
| 等于 | == != |
左到右 |
| 按位與 | & |
左到右 |
| 位異或 | ^ |
左到右 |
| 按位或 | / |
左到右 |
| 邏輯與 | && |
左到右 |
| 邏輯或 | // |
左到右 |
| 條件 | ?: |
右到左 |
| 賦值 | = += -= *= /= %=>>= <<= &= ^= /= |
右到左 |
| 逗號(hào) | , |
左到右 |
示例: 創(chuàng)建一個(gè)源文件: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;
}
執(zhí)行上面代碼,得到以下結(jié)果 -
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
請(qǐng)按任意鍵繼續(xù). . .