Operators
Operator is a symbol tells the compiler to perform a specific mathematical or logical operations. C language built a wealth of operators, and provides the following types of operators:
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Miscellaneous Operators
Arithmetic Operators
The following table shows all the arithmetic operators in C language support. Suppose the variable A is 10, the variable B is 20, then:
Operators | description | Examples |
---|---|---|
+ | The two operands are added | The resulting 30 A + B |
- | Subtracting the second operand from the first operand | A - B obtained -10 |
* | Multiplying two operands | The obtained A * B 200 |
/ | Numerator by the denominator | B / A to give 2 |
% | Modulo operator, the remainder after divisible | B% A 0 obtained |
++ | Increment operator, an integer value is incremented by 1 | A ++ will get 11 |
-- | Decrement operator, reducing the integer 1 | A-- will give 9 |
a ++ and ++ a different
a ++ is the first assignment operator
++ after a first operation is assigned
Relational Operators
The following table shows the C language supported by all relational operators. Suppose the variable A is 10, the variable B is 20, then:
Operators | description | Examples |
---|---|---|
== | Check the value of two operands are equal, it is equal if the condition is true. | (A == B) is false. |
!= | Check the value of two operands are equal, if not equal condition is true. | (A! = B) is true. |
> | Check whether the value of the left operand is greater than right operand values, if the condition is true. | (A> B) is false. |
< | Check the value of the left operand is less than right operand value, if the condition is true. | (A <B) is true. |
>= | Check whether the value of the left operand is greater than or equal to right operand, if the condition is true. | (A> = B) is false. |
<= | Check the value of the left operand is less than or equal to the value of the right operand, if the condition is true. | (A <= B) is true. |
Logical Operators
The following table shows the relationship between the C language supported by all logical operators. Assume that the variable A has the value 1, the variable B is 0, then:
Operators | description | Examples |
---|---|---|
&& | Called logical AND operator. If both operands are zero, the condition is true. | (A && B) is false. |
|| | Called logical OR operator. If both operands have any non-zero, then the condition is true. | (A || B) is true. |
! | Called logical NOT operator. For reversing the logic state of the operand. If the condition is true the logic NOT operator will make false. | !(A && B) 为真。 |
Bitwise Operators
Bitwise operators acting on the bit, and perform bitwise operations.
If assumed that A = 60, and B = 13, now in binary format represented, they are as follows:
A = 0011 1100
B = 0000 1101
// 上下为一组观察
-----------------
A&B = 0000 1100 // 都1才1
A|B = 0011 1101 // 有1就1
A^B = 0011 0001 // 有1为1,都1为0
~A = 1100 0011 // 0为1,1为0(反过来)
Operators | description | Examples |
---|---|---|
<< | Binary left shift operator. All bits of each of the left operand a number of bits (binary digits discarded left, the right complement 0). | A << 2 The resulting 240, i.e. 11,110,000 |
>> | Binary right shift operator. All bits of each of a number of a number of bits to the right, the left complement positive numbers 0, 1 negative fill the left and right discarded. | A >> 2 15 obtained, i.e. 0000 1111 |
Examples
See examples below for all the available C language bitwise operators:
#include <stdio.h>
int 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 - c 的值是 %d\n", c );
c = a | b; /* 61 = 0011 1101 */
printf("Line 2 - c 的值是 %d\n", c );
c = a ^ b; /* 49 = 0011 0001 */
printf("Line 3 - c 的值是 %d\n", c );
c = ~a; /*-61 = 1100 0011 */
printf("Line 4 - c 的值是 %d\n", c );
c = a << 2; /* 240 = 1111 0000 */
printf("Line 5 - c 的值是 %d\n", c );
c = a >> 2; /* 15 = 0000 1111 */
printf("Line 6 - c 的值是 %d\n", c );
}
When the above code is compiled and executed, it produces the following results:
Line 1 - The value of c is 12 is
Line 2 - the value of c is 61 is
Line. 3 - The value of c is 49
Line. 4 - The value of c is -61
Line. 5 - c is a 240
Line. 6 - 15 is the value of c
Assignment Operators
The following table lists the C language supported by the assignment operator:
Operators | description | Examples |
---|---|---|
= | Simple assignment operator, the values assigned to right-hand operand left operand | A + C = A + B value B will be assigned to C |
+= | Was added and the assignment operator, the result of adding the number to the right of the left operand operations assigned to the left operand | C + = A corresponding to C = C + A |
-= | Save and assignment operator, the left minus the right operand operations assigned to the left operand results | C - = A corresponds to the C = C - A |
*= | And by the assignment operator, the right operand the result of multiplying the left operand assigned to left operand | C = C = A corresponds C A |
/= | In addition result and the assignment operator, the left operand is divided by the right operand to the left operand assignment | C / = A equivalent C = C / A |
%= | And assignment modulo operator, find two operands left operand assigned to the analog | C% = A corresponds to the C = C% A |
<<= | Left and assignment operator | C << = 2 is equivalent to the C = C << 2 |
>>= | Right shift and assignment operator | C >> = 2 is equivalent to the C = C >> 2 |
&= | Bitwise AND assignment operator and | C & = 2 is equivalent to the C = C & 2 |
^= | Bitwise XOR assignment operator and | C ^ = 2 is equivalent to the C = C ^ 2 |
|= | And bitwise OR assignment operator | C | = 2 is equivalent to the C = C | 2 |
Examples
See examples below for all the available C language assignment operator:
#include <stdio.h>
main()
{
int a = 21;
int c ;
c = a;
printf("Line 1 - = 运算符实例,c 的值 = %d\n", c );
c += a;
printf("Line 2 - += 运算符实例,c 的值 = %d\n", c );
c -= a;
printf("Line 3 - -= 运算符实例,c 的值 = %d\n", c );
c *= a;
printf("Line 4 - *= 运算符实例,c 的值 = %d\n", c );
c /= a;
printf("Line 5 - /= 运算符实例,c 的值 = %d\n", c );
c = 200;
c %= a;
printf("Line 6 - %= 运算符实例,c 的值 = %d\n", c );
c <<= 2;
printf("Line 7 - <<= 运算符实例,c 的值 = %d\n", c );
c >>= 2;
printf("Line 8 - >>= 运算符实例,c 的值 = %d\n", c );
c &= 2;
printf("Line 9 - &= 运算符实例,c 的值 = %d\n", c );
c ^= 2;
printf("Line 10 - ^= 运算符实例,c 的值 = %d\n", c );
c |= 2;
printf("Line 11 - |= 运算符实例,c 的值 = %d\n", c );
}
When the above code is compiled and executed, it produces the following results:
Line 1 - = 运算符实例,c 的值 = 21
Line 2 - += 运算符实例,c 的值 = 42
Line 3 - -= 运算符实例,c 的值 = 21
Line 4 - *= 运算符实例,c 的值 = 441
Line 5 - /= 运算符实例,c 的值 = 21
Line 6 - %= 运算符实例,c 的值 = 11
Line 7 - <<= 运算符实例,c 的值 = 44
Line 8 - >>= 运算符实例,c 的值 = 11
Line 9 - &= 运算符实例,c 的值 = 2
Line 10 - ^= 运算符实例,c 的值 = 0
Line 11 - |= 运算符实例,c 的值 = 2
杂项运算符 ↦ sizeof & 三元
下表列出了 C 语言支持的其他一些重要的运算符,包括 sizeof 和 ? :。
运算符 | 描述 | 实例 |
---|---|---|
sizeof() | 返回变量的大小。 | sizeof(a) 将返回 4,其中 a 是整数。 |
& | 返回变量的地址。 | &a; 将给出变量的实际地址。 |
* | 指向一个变量。 | *a; 将指向一个变量。 |
? : | 条件表达式 | 如果条件为真 ? 则值为 X : 否则值为 Y |
实例
请看下面的实例,了解 C 语言中所有可用的杂项运算符:
#include <stdio.h>
int main()
{
int a = 4;
short b;
double c;
int* ptr;
/* sizeof 运算符实例 */
printf("Line 1 - 变量 a 的大小 = %lu\n", sizeof(a) );
printf("Line 2 - 变量 b 的大小 = %lu\n", sizeof(b) );
printf("Line 3 - 变量 c 的大小 = %lu\n", sizeof(c) );
/* & 和 * 运算符实例 */
ptr = &a; /* 'ptr' 现在包含 'a' 的地址 */
printf("a 的值是 %d\n", a);
printf("*ptr 是 %d\n", *ptr);
/* 三元运算符实例 */
a = 10;
b = (a == 1) ? 20: 30;
printf( "b 的值是 %d\n", b );
b = (a == 10) ? 20: 30;
printf( "b 的值是 %d\n", b );
}
当上面的代码被编译和执行时,它会产生下列结果:
Line 1 - 变量 a 的大小 = 4
Line 2 - 变量 b 的大小 = 2
Line 3 - 变量 c 的大小 = 8
a 的值是 4
*ptr 是 4
b 的值是 30
b 的值是 20
C 中的运算符优先级
运算符的优先级会影响到一个表达式如何计算。某些运算符比其他运算符有更高的优先级,例如,乘除运算符具有比加减运算符更高的优先级。
下表将按运算符优先级从高到低列出各个运算符,在表达式中,较高优先级的运算符会优先被计算。
类别 | 运算符 | 结合性 |
---|---|---|
后缀 | () [] -> . ++ - - | 从左到右 |
一元 | + - ! ~ ++ - - (type)* & sizeof | 从右到左 |
乘除 | * / % | 从左到右 |
加减 | + - | 从左到右 |
移位 | << >> | 从左到右 |
关系 | < <= > >= | 从左到右 |
相等 | == != | 从左到右 |
位与 AND | & | 从左到右 |
位异或 XOR | ^ | 从左到右 |
位或 OR | | | 从左到右 |
逻辑与 AND | && | 从左到右 |
逻辑或 OR | || | 从左到右 |
条件 | ?: | 从右到左 |
赋值 | = += -= *= /= %=>>= <<= &= ^= |= | 从右到左 |
逗号 | , | 从左到右 |