运算符是告诉编译器执行特定数学或逻辑功能的符号。
- 算术运算符
- 关系运算符
- 逻辑运算符
- 按位运算符
- 赋值运算符
- 杂项运算符
算术运算符
下表显示了C语言支持的所有算术运算符。假设变量A=10,变量B=20,则-
操作符 | 描述 | 例 |
---|---|---|
+ | 加两个操作数。 | A + B = 30 |
- | 从第一个减去第二个操作数。 | A − B = -10 |
* | 将两个操作数相乘。 | A * B = 200 |
/ | 将分子除以除分子。 | B / A = 2 |
% | 模运算符和整数除法后的余数。 | B%A = 0 |
++ | 增量运算符将整数值增加一。 | A ++ = 11 |
-- | 减量运算符将整数值减一。 | A-- = 9 |
#include <stdio.h>
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 );
c = --a;
printf("Line 8 - Value of c is %d\n", c );
c = a--;
printf("Line 9 - 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
Line 8 - Value of c is 20
Line 9 - Value of c is 21
关系运算符
下表显示了C支持的所有关系运算符。假设变量A持有10,变量B持有20,则-
操作符 | 描述 | 例 |
---|---|---|
== | 检查两个操作数的值是否相等。如果是,则条件变为真。 | (A == B)为假。 |
!= | 检查两个操作数的值是否相等。如果值不相等,则条件为真。 | (A!= B)为真。 |
> | 检查左操作数的值是否大于右操作数的值。如果是,则条件变为真。 | (A> B)为假。 |
< | 检查左操作数的值是否小于右操作数的值。如果是,则条件变为真。 | (A <B)为真。 |
> = | 检查左操作数的值是否大于或等于右操作数的值。如果是,则条件变为真。 | (A> = B)为假。 |
<= | 检查左操作数的值是否小于或等于右操作数的值。如果是,则条件变为真。 | (A <= B)为真。 |
#include <stdio.h>
main() {
int a = 21;
int b = 10;
int c ;
//输出:Line 1 - a is not equal to b
if( a == b ) {
printf("Line 1 - a is equal to b\n" );
} else {
printf("Line 1 - a is not equal to b\n" );
}
//输出:Line 2 - a is not less than b
if ( a < b ) {
printf("Line 2 - a is less than b\n" );
} else {
printf("Line 2 - a is not less than b\n" );
}
//输出:Line 3 - a is greater than b
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;
//输出:Line 4 - a is either less than or equal to b
if ( a <= b ) {
printf("Line 4 - a is either less than or equal to b\n" );
}
//输出:Line 5 - b is either greater than or equal to b
if ( b >= a ) {
printf("Line 5 - b is either greater than or equal to b\n" );
}
}
逻辑运算符
下表显示了C语言支持的所有逻辑运算符。假设变量A持有1,变量B持有0,那么-
操作符 | 描述 | 例 |
---|---|---|
&& | 称为逻辑真运算符。如果两个操作数都不为零,则条件变为true。 | (A && B)为假 |
|| | 称为逻辑或运算符。如果两个操作数中的任何一个都不为零,则条件变为true。 | (A || B)为真。 |
! | 称为逻辑非运算符。它用于反转其操作数的逻辑状态。如果条件为真,则逻辑非运算符会将其设置为假。 | !(A && B)为真。 |
按位运算符
按位运算符对位进行运算并执行逐位操作。&,|和^的真值表如下-
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 |
下表列出了C支持的按位运算符。假设变量'A'=60(0011 1100),变量'B'=13(0000 1101),然后-
操作符 | 描述 | 例 |
---|---|---|
& | 两个操作数进行二进制对位&运算。 | (A&B)= 12,即0000 1100 |
| | 两个操作数进行二进制对位|运算 | (A | B)= 61,即0011 1101 |
^ | 两个操作数进行二进制对位^运算。 | (A ^ B)= 49,即0011 0001 |
〜 | 二进制补码运算符是一元的,具有“翻转”位的作用。 | (〜A)=〜(60),即 1100 0011 |
<< | 二进制左移运算符。左操作数的值向左移动右操作数指定的位数。 | A << 2 = 240,即1111 0000 |
>> | 二进制右移运算符。左操作数的值向右移动右操作数指定的位数。 | A >> 2 = 15,即0000 1111 |
#include <stdio.h>
main() {
unsigned int a = 60; /* 60 = 0011 1100 */
unsigned int b = 13; /* 13 = 0000 1101 */
int c = 0;
//输出:Line 1 - Value of c is 12
c = a & b; /* 12 = 0000 1100 */
printf("Line 1 - Value of c is %d\n", c );
//输出:Line 2 - Value of c is 61
c = a | b; /* 61 = 0011 1101 */
printf("Line 2 - Value of c is %d\n", c );
//输出:Line 3 - Value of c is 49
c = a ^ b; /* 49 = 0011 0001 */
printf("Line 3 - Value of c is %d\n", c );
//输出:Line 4 - Value of c is -61
c = ~a; /*-61 = 1100 0011 */
printf("Line 4 - Value of c is %d\n", c );
//输出:Line 5 - Value of c is 240
c = a << 2; /* 240 = 1111 0000 */
printf("Line 5 - Value of c is %d\n", c );
//输出:Line 6 - Value of c is 15
c = a >> 2; /* 15 = 0000 1111 */
printf("Line 6 - Value of c is %d\n", 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 | = 2与C = C |相同 2 |
杂项运算符↦sizeof和三目运算符
操作符 | 描述 | 例 |
---|---|---|
sizeof() | 返回变量的大小。 | sizeof(a),其中a为整数,将返回4。 |
& | 返回变量的地址。 | &a; 返回变量的实际地址。 |
* | 指向变量的指针。 | *a; |
?: | 条件表达式。 | 如果条件为真?然后取值X:否则取值Y |
#include <stdio.h> 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