C Operators

An operator is a symbol that tells the compiler to perform specific mathematical or logical operations. C language has a rich set of built-in operators and provides the following types of operators:

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators
  • Miscellaneous Operators

This chapter will introduce arithmetic operators, relational operators, logical operators, bitwise operators, assignment operators, and other operators one by one.

Arithmetic Operators

The following table shows all arithmetic operators supported by C language. Assume variablesAhas a value of 10, variableBhas a value of 20, then:

OperatorsDescriptionExample
+Adds two operands.A + B will give 30
-Subtract the second operand from the first operand.A - B will give -10
*Multiplies two operands.A * B will give 200
/Numerator divided by denominatorB / A will give 2
%Modulus operator, the remainder after integer division.B % A will give 0
++Increment operator, increases the integer value by 1.A++ will give 11
--Decrement operator, decreases the integer value by 1.A-- will give 9

Example

See the following example to learn all available arithmetic operators in C:

Example

#include <stdio.h> int 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++; // increment after assignment, c is 21, a is 22 printf("Line 6 - value of c is %d\n", c ); c = a--; // decrement after assignment, c is 22, a is 21 printf("Line 7 - value of c is %d\n", c ); }

When the above code is compiled and executed, it produces the following result:

Line 1 - c 的值是 31
Line 2 - c 的值是 11
Line 3 - c 的值是 210
Line 4 - c 的值是 2
Line 5 - c 的值是 1
Line 6 - c 的值是 21
Line 7 - c 的值是 22

The following example demonstrates the difference between a++ and ++a:

Example

#include <stdio.h> int main() { int c; int a = 10; c = a++; printf("Assignment before operation:\n"); printf("Line 1 - value of c is %d\n", c ); printf("Line 2 - value of a is %d\n", a ); a = 10; c = a--; printf("Line 3 - value of c is %d\n", c ); printf("Line 4 - value of a is %d\n", a ); printf("Operation before assignment:\n"); a = 10; c = ++a; printf("Line 5 - value of c is %d\n", c ); printf("Line 6 - value of a is %d\n", a ); a = 10; c = --a; printf("Line 7 - value of c is %d\n", c ); printf("Line 8 - value of a is %d\n", a ); }

The output of the above program execution is:

先赋值后运算:
Line 1 - c 的值是 10
Line 2 - a 的值是 11
Line 3 - c 的值是 10
Line 4 - a 的值是 9
先运算后赋值:
Line 5 - c 的值是 11
Line 6 - a 的值是 11
Line 7 - c 的值是 9
Line 8 - a 的值是 9

Relational Operators

The following table shows all relational operators supported by C language. Assume variablesAhas a value of 10, variableBhas a value of 20, then:

OperatorsDescriptionExample
==Checks whether the values of two operands are equal. If they are equal, the condition is true.(A == B) is false.
!=Checks whether the values of two operands are equal. If they are not equal, the condition is true.(A != B) is true.
>Checks whether the value of the left operand is greater than the value of the right operand. If so, the condition is true.(A > B) is false.
<Checks whether the value of the left operand is less than the value of the right operand. If so, the condition is true.(A < B) is true.
>=Checks whether the value of the left operand is greater than or equal to the value of the right operand. If so, the condition is true.(A >= B) is false.
<=Checks whether the value of the left operand is less than or equal to the value of the right operand. If so, the condition is true.(A <= B) is true.

Example

See the following example to learn all available relational operators in C:

Example

#include <stdio.h> int main() { int a = 21; int b = 10; int c ; if( a == b ) { printf("Line 1 - a equals 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" ); } /*Change the values of a and b*/ a = 5; b = 20; if ( a <= b ) { printf("Line 4 - a is less than or equal to b\n" ); } if ( b >= a ) { printf("Line 5 - b is greater than or equal to a\n" ); } }

When the above code is compiled and executed, it produces the following result:

Line 1 - a 不等于 b
Line 2 - a 不小于 b
Line 3 - a 大于 b
Line 4 - a 小于或等于 b
Line 5 - b 大于或等于 a

Logical Operators

The following table shows all relational logical operators supported by C language. Assume variablesAhas a value of 1, variableBhas a value of 0, then:

OperatorsDescriptionExample
&&Called the logical AND operator. If both operands are non-zero, the condition is true.(A && B) is false.
||Called the logical OR operator. If either of the two operands is non-zero, the condition is true.(A || B) is true.
!Called the logical NOT operator. It is used to reverse the logical state of an operand. If a condition is true, the logical NOT operator will make it false.!(A && B) is true.

Example

See the following example to learn all available logical operators in C:

Example

#include <stdio.h> int 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" ); } /*Change the values of a and b*/ a = 0; b = 10; if ( a && b ) { printf("Line 3 - Condition is true\n" ); } else { printf("Line 3 - condition is false\n" ); } if ( !(a && b) ) { printf("Line 4 - condition is true\n" ); } }

When the above code is compiled and executed, it produces the following result:

Line 1 - 条件为真
Line 2 - 条件为真
Line 3 - 条件为假
Line 4 - 条件为真

Bitwise Operators

Bitwise operators act on bits and perform operations bit by bit. The truth tables of &, | and ^ are as follows:

pqp & qp | qp ^ q
00000
01011
11110
10011

Suppose A = 60 and B = 13. Now represented in binary format, they are as follows:

A = 0011 1100

B = 0000 1101

-----------------

A&B = 0000 1100

A|B = 0011 1101

A^B = 0011 0001

~A  = 1100 0011

The following table shows the bitwise operators supported by C language. Assume variablesAhas a value of 60, variableBhas a value of 13, then:

OperatorsDescriptionExample
&

Performs a logical AND operation on each bit of the two operands. If both corresponding bits are 1, the result is 1; otherwise, it is 0.

Bitwise AND operation performs the "AND" operation on binary bits. The rules:

0&0=0;   
0&1=0;    
1&0=0;     
1&1=1;
(A & B) evaluates to 12, which is 0000 1100.
|

Performs a logical OR operation on each bit of the two operands. If both corresponding bits are 0, the result is 0; otherwise, it is 1.

Bitwise OR operator performs the "OR" operation on binary bits. The rules:

0|0=0;   
0|1=1;   
1|0=1;    
1|1=1;
(A | B) evaluates to 61, which is 0011 1101.
^

Performs a logical XOR operation on each bit of the two operands. If the two corresponding bit values are the same, the result is 0; otherwise, it is 1.

XOR operator performs the "XOR" operation on binary bits. The rules:

0^0=0;   
0^1=1;   
1^0=1;  
1^1=0;
(A ^ B) evaluates to 49, which is 0011 0001.
~

Performs logical negation on each bit of the operand, that is, changes each 0 to 1 and each 1 to 0.

Bitwise NOT operator performs the "NOT" operation on binary bits. The rules:

~1=-2;   
~0=-1;
(~A ) will give -61, which is 1100 0011, the two's complement form of a signed binary number.
<<

Shifts all bits of the operand to the left by the specified number of bits. Shifting left by n bits is equivalent to multiplying by 2 to the power of n.

Binary left shift operator. Shifts all binary bits of an operand to the left by a certain number of bits (the leftmost binary bits are discarded, and zeros are filled in on the right).

A << 2 evaluates to 240, which is 1111 0000.
>>

Shifts all bits of the operand to the right by the specified number of bits. Shifting right by n bits is equivalent to dividing by 2 to the power of n.

Binary right shift operator. Shifts all binary bits of a number to the right by a certain number of bits. For positive numbers, zeros are filled on the left; for negative numbers, ones are filled on the left; the rightmost bits are discarded.

A >> 2 evaluates to 15, which is 0000 1111.

Example

See the following example to learn all available bitwise operators in C:

Example

#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 - 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 ); }

When the above code is compiled and executed, it produces the following result:

Line 1 - c 的值是 12
Line 2 - c 的值是 61
Line 3 - c 的值是 49
Line 4 - c 的值是 -61
Line 5 - c 的值是 240
Line 6 - c 的值是 15

Assignment Operators

The following table lists the assignment operators supported by C language:

OperatorsDescriptionExample
=Simple assignment operator, assigns the value of the right operand to the left operand.C = A + B will assign the value of A + B to C
+=Add and assignment operator: adds the right operand to the left operand and assigns the result to the left operand.C += A is equivalent to C = C + A.
-=Subtract and assignment operator: subtracts the right operand from the left operand and assigns the result to the left operand.C -= A is equivalent to C = C - A.
*=Multiply and assignment operator: multiplies the right operand by the left operand and assigns the result to the left operand.C *= A is equivalent to C = C * A.
/=Divide and assignment operator: divides the left operand by the right operand and assigns the result to the left operand.C /= A is equivalent to C = C / A.
%=Modulus and assignment operator, takes the modulus of two operands and assigns it to the left operand.C %= A is equivalent to C = C % A.
<<=Left shift and assignment operator.C <<= 2 is equivalent to C = C << 2.
>>=Right shift and assignment operator.C >>= 2 is equivalent to C = C >> 2.
&=Bitwise AND and assignment operator.C &= 2 is equivalent to C = C & 2.
^=Bitwise XOR and assignment operator.C ^= 2 is equivalent to C = C ^ 2
|=Bitwise OR and assignment operator.C |= 2 is equivalent to C = C | 2

Example

See the following example to learn all available assignment operators in C:

Example

#include <stdio.h> int main() { int a = 21; int c ; c = a; printf("= operator example, value of c = %d\n", c ); c += a; printf("+= operator example, value of c = %d\n", c ); c -= a; printf("-= operator example, value of c = %d\n", c ); c *= a; printf("*= operator example, value of c = %d\n", c ); c /= a; printf("/= operator example, value of c = %d\n", c ); c = 200; c %= a; printf("%%= operator example, value of c = %d\n", c ); c <<= 2; printf("<<= operator example, value of c = %d\n", c ); c >>= 2; printf(">>= operator example, value of c = %d\n", c ); c &= 2; printf("&= operator example, value of c = %d\n", c ); c ^= 2; printf("^= operator example, value of c = %d\n", c ); c |= 2; printf("|= operator example, value of c = %d\n", c ); }

When the above code is compiled and executed, it produces the following result:

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

Miscellaneous operators ↦ sizeof & ternary

The following table lists some other important operators supported by C language, includingsizeofand? :。

OperatorsDescriptionExample
sizeof()Returns the size of a variable.sizeof(a) will return 4, where a is an integer.
&Returns the address of a variable.&a; will give the actual address of the variable.
*Pointing to a variable.*a; will point to a variable.
? :Conditional expressionIf the condition is true ? then value X : otherwise value Y

Example

See the following example to learn all available miscellaneous operators in C:

Example

#include <stdio.h> int main() { int a = 4; short b; double c; int* ptr; /*sizeof operator example*/ printf("Line 1 - size of variable a = %lu\n", sizeof(a) ); printf("Line 2 - size of variable b = %lu\n", sizeof(b) ); printf("Line 3 - size of variable c = %lu\n", sizeof(c) ); /*& and * operator example*/ ptr = &a; /*'ptr' now contains the address of 'a'*/ printf("the value of a is %d\n", a); printf("*ptr is %d\n", *ptr); /*ternary operator example*/ a = 10; b = (a == 1) ? 20: 30; printf( "the value of b is %d\n", b ); b = (a == 10) ? 20: 30; printf( "the value of b is %d\n", b ); }

When the above code is compiled and executed, it produces the following result:

Line 1 - 变量 a 的大小 = 4
Line 2 - 变量 b 的大小 = 2
Line 3 - 变量 c 的大小 = 8
a 的值是 4
*ptr 是 4
b 的值是 30
b 的值是 20

Operator precedence in C

Operator precedence determines the grouping of terms in an expression. This affects how an expression is evaluated. Certain operators have higher precedence than others; for example, multiplication and division operators have higher precedence than addition and subtraction operators.

For example, x = 7 + 3 * 2. Here, x is assigned 13, not 20, because the operator * has higher precedence than +, so the multiplication 3*2 is calculated first, and then 7 is added.

The following table lists operators in order of precedence from highest to lowest. Operators with higher precedence appear at the top of the table, and operators with lower precedence appear at the bottom. In an expression, operators with higher precedence are evaluated first.

Category OperatorsAssociativity
Postfix() [] -> . ++ - -   Left to right
Unary + - ! ~ ++ - - (type)* & sizeof  Right to left
Multiplicative * / % Left to right
Additive+ -  Left to right
Shift << >>  Left to right
Relationship< <= > >=  Left to right
Equality == !=  Left to right
Bitwise AND&  Left to right
Bitwise XOR ^  Left to right
Bitwise OR |  Left to right
Logical AND&&  Left to right
Logical OR ||  Left to right
Conditions?:  Right to left
Assignment = += -= *= /= %=>>= <<= &= ^= |= Right to left
comma ,  Left to right

Example

See the following example to understand the precedence of operators in C:

Example

#include <stdio.h> int main() { int a = 20; int b = 10; int c = 15; int d = 5; int e; e = (a + b) * c / d; // ( 30 * 15 ) / 5 printf("the value of (a + b) * c / d is %d\n", e ); e = ((a + b) * c) / d; // (30 * 15 ) / 5 printf("the value of ((a + b) * c) / d is %d\n" , e ); e = (a + b) * (c / d); // (30) * (15/5) printf("the value of (a + b) * (c / d) is %d\n", e ); e = a + (b * c) / d; // 20 + (150/5) printf("the value of a + (b * c) / d is %d\n" , e ); return 0; }

When the above code is compiled and executed, it produces the following result:

(a + b) * c / d 的值是 90
((a + b) * c) / d 的值是 90
(a + b) * (c / d) 的值是 90
a + (b * c) / d 的值是 50
other extensions