English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C Language Basic Tutorial

C Language Flow Control

C Language Functions

C Language Arrays

C Language Pointers

C Language Strings

C Language Structure

C Language File

C Others

C Language Reference Manual

C Language Operators

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

  • Arithmetic operator

  • Relational operator

  • 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 operator

The table below shows all the arithmetic operators supported by the C language. Assume the variable A The value is 10, variable B The value is 20,则:

operatorDescriptionExample
+Add the two operandsA + B will get 30
-Subtract the second operand from the first operandA - B will get -10
*Multiply the two operandsA * B will get 200
/The numerator is divided by the denominatorB / A will get 2
%Modulus operator, the remainder after integer divisionB % A will get 0
++Increment operator, the integer value increases 1A++ You will get 11
--Decrement operator, the integer value decreases 1A-- You will get 9

Example

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

#include <stdio.h>
 
int main()
{
   int a = 21;
   int b = 10;
   int c;
 
   c = a + b;
   printf("Line 1 - c = a >>

   c = a - b;
   printf("Line 2 - c = a >>

   c = a * b;
   printf("Line 3 - c = a >>

   c = a / b;
   printf("Line 4 - c = a >>

   c = a % b;
   printf("Line 5 - c = a >>

   c = a++;  // Add after assignment 1 , c is 21, a is 22
   printf("Line 6 - c = a >>

   c = a--;  // Subtract after assignment 1 , c is 22 , a is 21
   printf("Line 7 - c = a >>

 
}

When the above code is compiled and executed, it will produce the following results:

Line 1 - The value of c is 31
Line 2 - The value of c is 11
Line 3 - The value of c is 210
Line 4 - The value of c is 2
Line 5 - The value of c is 1
Line 6 - The value of c is 21
Line 7 - The value of c is 22

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

#include <stdio.h>
 
int main()
{
   int c;
   int a = 10;
   c = a++; 
   printf("Assignment first, then operation:\n");
   printf("Line 1 - c = a >>

   printf("Line 2 - a's value is %d\n
   a = 10;
   c = a--; 
   printf("Line 3 - c = a >>

   printf("Line 4 - a's value is %d\n
 
   printf("Operation first, then assignment:\n");
   a = 10;
   c = ++a; 
   printf("Line 5 - c = a >>

   printf("Line 6 - a's value is %d\n
   a = 10;
   c = --a; 
   printf("Line 7 - c = a >>

   printf("Line 8 - a's value is %d\n
 
}

The output result of the above program is:

Assignment first, then operation:
Line 1 - The value of c is 10
Line 2 - The value of a is 11
Line 3 - The value of c is 10
Line 4 - The value of a is 9
Operation first, then assignment:
Line 5 - The value of c is 11
Line 6 - The value of a is 11
Line 7 - The value of c is 9
Line 8 - The value of a is 9

Relational operator

The table below shows all the relational operators supported by the C language. Assume the variable A The value is 10, variable B The value is 20,则:

operatorDescriptionExample
0, then:==Check if the values of the two operands are equal, if equal, the condition is true.
(A == B) is false.!=Check if the values of the two operands are equal, if not equal, the condition is true.
(A != B) is true.>Check if 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.<Check if 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.>=Check if 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.<=Check if the value of the left operand is less than or equal to the value of the right operand, if so, the condition is true.

Example

(A <= B) is true.

#include <stdio.h>
 
int main()
{
   int a = 21;
   int b = 10;
   int c;
 
   See the following examples to understand all the available relational operators in C language:
   {
      printf("Line 1 - if( a == b )
   }
   else
   {
      printf("Line 1 - a is equal to b
" );
   }
   a is not equal to b
" );
   {
      printf("Line 2 - if ( a < b )
   }
   else
   {
      printf("Line 2 - a is less than b
" );
   }
   a is not less than b
" );
   {
      printf("Line 3 - if ( a > b )
   }
   else
   {
      printf("Line 3 - a is greater than b
" );
   }
   /* Change the values of a and b */
   a = 5;
   b = 20;
   a is not greater than b
" );
   {
      printf("Line 4 - a is less than or equal to b
" );
   }
   if ( b >= a )
   {
      printf("Line 5 - b is greater than or equal to a
" );
   }
}

When the above code is compiled and executed, it will produce the following results:

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 less than or equal to b
Line 5 - b is greater than or equal to a

Logical operators

The following table shows all the relational logical operators supported by C language. Assuming variable A The value is 1, variable B If the value is 0, then:

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

Example

See the following examples to understand all the available logical operators in C language:

#include <stdio.h>
 
int main()
{
   int a = 5;
   int b = 20;
   int c;
 
   if ( a && b )
   {
      printf("Line 1 - Condition is true
" );
   }
   if ( a || b )
   {
      printf("Line 2 - Condition is true
" );
   }
   /* Change the values of a and b */
   a = 0;
   b = 10;
   if ( a && b )
   {
      printf("Line 3 - Condition is true
" );
   }
   else
   {
      printf("Line 3 - Condition is false
" );
   }
   if ( !(a && b) )
   {
      printf("Line 4 - Condition is true
" );
   }
}

When the above code is compiled and executed, it will produce the following results:

Line 1 - Condition is true
Line 2 - Condition is true
Line 3 - Condition is false
Line 4 - Condition is true

Bitwise operators

Bitwise operators operate on bits and perform operations bit by bit. The truth tables for &、 | and ^ are as follows:

pqp & qp | qp ^ q
00000
01011
11110
10011

Assuming if A = 60, and B = 13Now 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 the C language. Assume the variable A The value is 60, variable B The value is 13Then:

operatorDescriptionExample
&

Bitwise AND operation, performs a 'bitwise AND' operation. Operation rules:

0&0=0;   
0&1=0;    
1&0=0;     
1&1=1;
(A & B) will get 12That is, 0000 1100
|

Bitwise OR operator, performs a 'bitwise OR' operation. Operation rules:

0|0=0;   
0|1=1;   
1|0=1;    
1|1=1;
(A | B) will get 61That is, 0011 1101
^

XOR operator, performs a 'bitwise XOR' operation. Operation rules:

0^0=0;   
0^1=1;   
1^0=1;  
1^1=0;
(A ^ B) will get 49That is, 0011 0001
~

Negation operator, performs a 'bitwise negation' operation. Operation rules:

~1=0;   
~0=1;
(~A) will get -61That is 1100 0011That is, the two's complement form of a signed binary number.
<<Binary left shift operator. Shifts all binary bits of an operand to the left by a certain number of positions (the leftmost bits are discarded, and 0 is filled on the right).A << 2 You will get 240, that is 1111 0000
>>Binary right shift operator. Shifts all binary bits of a number to the right by a certain number of positions, filling with 0 on the left for positive numbers and with the sign bit for negative numbers.1The rightmost bits are discarded.A >> 2 You will get 15That is, 0000 1111

Example

See the following examples to understand all the bitwise operators available in the C language:

#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 = a >>

 
   c = a | b;       /* 61 = 0011 1101 */
   printf("Line 2 - c = a >>

 
   c = a ^ b;       /* 49 = 0011 0001 */
   printf("Line 3 - c = a >>

 
   c = ~a;          /*-61 = 1100 0011 */
   printf("Line 4 - c = a >>

 
   c = a << 2;     /* 240 = 00 1111 0000 */
   printf("Line 5 - c = a >>

 
   c = a >> 2;     /* 15 = 0000 1111 */
   printf("Line 6 - c = a >>

}

When the above code is compiled and executed, it will produce the following results:

Line 1 - The value of c is 12
Line 2 - The value of c is 61
Line 3 - The value of c is 49
Line 4 - The value of c is -61
Line 5 - The value of c is 240
Line 6 - The value of c is 15

Assignment operators

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

operatorDescriptionExample
=Simple assignment operator, assigns the value of the right operand to the left operandC = A + B will assign A + The value of B is assigned to C
+=Addition and assignment operator, assigns the result of adding the right operand to the left operand to the left operandC += A is equivalent to C = C + A
-=Subtraction and assignment operator, assigns the result of subtracting the right operand from the left operand to the left operandC -= A is equivalent to C = C - A
*=Multiplication and assignment operator, assigns the result of multiplying the right operand by the left operand to the left operandC *= A is equivalent to C = C * A
/=Division and assignment operator, assigns the result of dividing the left operand by the right operand to the left operandC /= A is equivalent to C = C / A
%=Modulo and assignment operator, assigns the modulus of two operands to the left operandC %= A is equivalent to C = C % A
<<=Left shift and assignment operatorC <<= 2 Equivalent to C = C << 2
>>=Right shift and assignment operatorC >>= 2 Equivalent to C = C >> 2
&=Bitwise AND and assignment operatorC &= 2 Equivalent to C = C & 2
^=Bitwise XOR and assignment operatorC ^= 2 Equivalent to C = C ^ 2
|=Bitwise OR and assignment operatorC |= 2 Equivalent to C = C | 2

Example

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

#include <stdio.h>
 
main()
{
   int a = 21;
   int c;
 
   c =
   printf("Line 1 - Example of = operator, the value of c = %d\n", c);
 
   c +=
   printf("Line 2 - +Example of = operator, the value of c = %d\n", c);
 
   c -=
   printf("Line 3 - -Example of = operator, the value of c = %d\n", c);
 
   c *=
   printf("Line 4 - *Example of = operator, the value of c = %d\n", c);
 
   c /=
   printf("Line 5 - /Example of = operator, the value of c = %d\n", c);
 
   c = 200;
   c %=
   printf("Line 6 - Example of %= operator, the value of c = %d\n", c);
 
   c <<=  2;
   printf("Line 7 - Example of <<= operator, the value of c = %d\n", c);
 
   c >>=  2;
   printf("Line 8 - Example of >== operator, the value of c = %d\n", c);
 
   c &=  2;
   printf("Line 9 - Example of &= operator, the value of c = %d\n", c);
 
   c ^=  2;
   printf("Line 10 - Example of ^= operator, the value of c = %d\n", c);
 
   c |=  2;
   printf("Line 11 - Example of |= operator, the value of c = %d\n", c);
 
}

When the above code is compiled and executed, it will produce the following results:

Line 1 - Example of = operator, the value of c = 21
Line 2 - +Example of = operator, the value of c = 42
Line 3 - -Example of = operator, the value of c = 21
Line 4 - *Example of = operator, the value of c = 441
Line 5 - /Example of = operator, the value of c = 21
Line 6 - Example of %= operator, the value of c = 11
Line 7 - Example of <<= operator, the value of c = 44
Line 8 - Example of >== operator, the value of c = 11
Line 9 - Example of &= operator, the value of c = 2
Line 10 - Example of ^= operator, the value of c = 0
Line 11 - Example of |= operator, the value of c = 2

Miscellaneous operators → sizeof & ternary

The following table lists some other important operators supported by C language, including sizeof And ? :.

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

Example

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

#include <stdio.h>
 
int main()
{
   int a = 4;
   short b;
   double c;
   int* ptr;
 
   /* Example of sizeof operator */
   printf("Line 1 - The size of variable a is %lu\n", sizeof(a));
   printf("Line 2 - The size of variable b = %lu\n", sizeof(b));
   printf("Line 3 - The size of variable c = %lu\n", sizeof(c));
 
   /* & and * Operator examples */
   ptr = &a;    /* 'ptr' now contains the address of 'a' */
   printf("The 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("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 will produce the following results:

Line 1 - The size of variable a = 4
Line 2 - The size of variable b = 2
Line 3 - The size of variable c = 8
The value of a is 4
*ptr is 4
The value of b is 30
The value of b is 20

Operator precedence in C

The precedence of operators determines how terms are combined in an expression, which affects how an expression is evaluated. Some 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 the value of 13, rather than 20, because the operator * has a precedence + higher precedence, so multiplication is calculated first 3*2, then in addition to 7.

The following table lists the operators from highest to lowest precedence, with higher precedence operators at the top and lower precedence operators at the bottom. In an expression, higher precedence operators are calculated first.

category operator associativity 
postfix () [] -> .  ++   - -  from left to right 
unary +  -   ! ~  ++  - -   (type)*  & sizeof from right to left 
multiplication and division *  /  % from left to right 
addition and subtraction +  - from left to right 
shift << >> from left to right 
relation < <= > >= from left to right 
equality == != from left to right 
bitwise and AND & from left to right 
bitwise xor XOR ^ from left to right 
bitwise or OR | from left to right 
logical and AND && from left to right 
logical or OR || from left to right 
condition ?: from right to left 
assignment =  +=  -=  *=  /= %=>>= <<= &= ^= |= from right to left 
comma , from left to right 

Example

Please see the following example to understand the precedence of operators in C language:

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

When the above code is compiled and executed, it will produce the following results:

(a + b) * c / d's value is 90
((a + b) * c) / d's value is 90
(a + b) * (c / d)'s value is 90
a + (b * c) / d's value is 50