Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

The C operator


May 11, 2021 C


Table of contents


The C operator

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

  • Arithmetic operator
  • The relationship operator
  • The logical operator
  • Bit operator
  • The assignment operator
  • Miscellaneous operator

This chapter describes arithmetic operators, relationship operators, logical operators, bit operators, assignment operators, and miscellaneous operators one by one.

Arithmetic operator

The following table shows all the arithmetic operators supported by the C language. Assuming that the value of variable A is 10 and the value of variable B is 20, then:

Operator describe Example
+ Add two operands A + B will get 30
- Split the second operand from the first operand A - B will get -10
* Multiply two operands A * B will get 200
/ Molecule division B / A will get 2
% Take an operator, the remainder after the removal B% a will get 0
++ Self-incremental operator, integer value increase 1 A ++ will get 11
-- Self-reduction operator, integer value reduction 1 A - will get 9

Instance

Look at the following example to learn about all the arithmetic operators available in the C language:

#include <stdio.h>

int main()
{
   int a = 21;
   int b = 10;
   int c ;

   c = a + b;
   printf("Line 1 - c 的值是 %d\n", c );
   c = a - b;
   printf("Line 2 - c 的值是 %d\n", c );
   c = a * b;
   printf("Line 3 - c 的值是 %d\n", c );
   c = a / b;
   printf("Line 4 - c 的值是 %d\n", c );
   c = a % b;
   printf("Line 5 - c 的值是 %d\n", c );
   c = a++;  // 赋值后再加 1 ,c 为 21,a 为 22
   printf("Line 6 - c 的值是 %d\n", c );
   c = a--;  // 赋值后再减 1 ,c 为 22 ,a 为 21
   printf("Line 7 - c 的值是 %d\n", c );

}

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

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 plus and a: the same points of a plus and a are given to a1, the difference is that a plus is assigned first and then 1, and the same point is assigned first.

#include <stdio.h>
int main()
{
int c;
int a = 10;
c = a++;
printf("先赋值后运算:\n");
printf("Line 1 - c 的值是 %d\n", c );
printf("Line 2 - a 的值是 %d\n", a );
a = 10;
c = a--;
printf("Line 3 - c 的值是 %d\n", c );
printf("Line 4 - a 的值是 %d\n", a );
printf("先运算后赋值:\n");
a = 10;
c = ++a;
printf("Line 5 - c 的值是 %d\n", c );
printf("Line 6 - a 的值是 %d\n", a );
a = 10;
c = --a;
printf("Line 7 - c 的值是 %d\n", c );
printf("Line 8 - a 的值是 %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

The relationship operator

The following table shows all the relationship operators supported by the C language. Assuming that the value of variable A is 10 and the value of variable B is 20, then:

Operator describe Example
== Check if the value of the two operands is equal, if equally, the condition is true. (A == b) is not true.
!= Check if the value of the two operands is equal, if it is 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 operation, if it is true. (A> b) is not true.
< Check if the value of the left operand is less than the value of the right operand, if it 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 it is true. (A> = b) is not true.
<= Check if the value of the left operand is less than or equal to the value of the right operand, if it is true. (A <= b) is true.

Instance

Look at the following example to learn about all the relationship operators available in the C language:

#include <stdio.h>

int main()
{
   int a = 21;
   int b = 10;
   int c ;

   if( a == b )
   {
      printf("Line 1 - a 等于 b\n" );
   }
   else
   {
      printf("Line 1 - a 不等于 b\n" );
   }
   if ( a < b )
    { 
            printf("Line 2 - a 小于 b\n" );
    }
    else
    {
            printf("Line 2 - a 不小于 b\n" );
    }
   if ( a > b )
   {
      printf("Line 3 - a 大于 b\n" );
   }
   else
   {
      printf("Line 3 - a 不大于 b\n" );
   }
   /* 改变 a 和 b 的值 */
   a = 5;
   b = 20;
   if ( a <= b )
    {       printf("Line 4 - a 小于或等于 b\n" );
    }
    if ( b >= a )
   {
      printf("Line 5 - b 大于或等于 b\n" );
   }
}

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

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

The logical operator

The following table shows all the relationship logic operators supported by the C language. Assuming that the value of variable A is 1 and the value of variable B is 0, then:

Operator describe Example
&& Logic and operators.If the two operands are not zero, the conditions are true. (A && b) is false.
|| Logic or operator.If there is any non-zero in the two operands, the condition is true. (A || b) is true.
! Logical non-operator.The logic state used to reverse the operand.If the condition is true, the logical non-operator will make it fake. ! (A && b) is true.

Instance

Look at the following example to learn about all the logical operators available in the C language:

#include <stdio.h>

int main()
{
   int a = 5;
   int b = 20;
   int c ;

   if ( a && b )
   {
      printf("Line 1 - 条件为真\n" );
   }
   if ( a || b )
   {
      printf("Line 2 - 条件为真\n" );
   }
   /* 改变 a 和 b 的值 */
   a = 0;
   b = 10;
   if ( a && b )
   {
      printf("Line 3 - 条件为真\n" );
   }
   else
   {
      printf("Line 3 - 条件不为真\n" );
   }
   if ( !(a && b) )
   {
      printf("Line 4 - 条件为真\n" );
   }
}

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

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

Bit operator

Bit operators act on bits and perform operations bit by bit. & amp;、 | The true value tables for and s are as follows:

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

Suppose that if A is 60 and B is 13, they are now represented in binary format, 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 bit operators supported by the C language. Assuming that the value of variable A is 60 and the value of variable B is 13, then:

Operator describe Example
& Bit and operation, "and" operation according to the binary position.Rules:
0&0=0;
0&1=0;
1&0=0;
1&1=1;
(A & B) will get 12, ie 0000 1100
| Bit or operators, press the binary position "or" operation.Rules:
0|0=0;
0|1=1;
1|0=1;
1|1=1;
(A | b) will get 61, ie 0011 1101
^ The different or operator, the "different or" operation is performed according to the binary position.Rules:
0^0=0;
0^1=1;
1^0=1;
1^1=0
(A ^ b) will get 49, ie 0011 0001
~ Take the counter operator and press the "reflex" operation according to the binary position.Rules:
~1=0;
~0=1;
(~ A) will result in -61, which is a complement form of 1100 0011, 2, a binary number with a symbol.
<< Binary left shift operator.The value of the left operation is moved to the left move the number of digits specified by the right operand (the binary bit on the left, and the right side is added to the right). A << 2 will get 240, that is, 1111 0000
>> Binary right shift operator.The value of the left operand is moved to the right number of digits (positive left to supplement 0, no number of negative numbers, and discard the right). A >> 2 will get 15, ie 0000 1111

Instance

Look at the following example to learn about all the bit 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 的值是 %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 - c 的值是 12
Line 2 - c 的值是 61
Line 3 - c 的值是 49
Line 4 - c 的值是 -61
Line 5 - c 的值是 240
Line 6 - c 的值是 15

The assignment operator

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

Operator describe Example
= Simple assignment operator, assign the value of the right operand to the left operand C = a + b will assign the value of the A + B to C
+= Adding the operator, add the result of the left operand to the left operand to the left operand C + = a corresponds to c = c + a
-= Reduce the value, subtract the left operand to the left operand to the left operand C - = a equivalent to c = c - a
*= Multiplication and assignment operators, multiply the right operation number by the result of the left operand to the left operand C * = a equivalent to c = c * a
/= Except and assign the operator, divide the left operand to assign the left operand to the left operand C / = a corresponds to c = c / a
%= Dammon and assign the operator, ask the two operands to give the left operand C% = a corresponds to c = c% a
<<= Left and assignment operator C << = 2 equivalent to C = c << 2
>>= Right shift and assignment operator C >> = 2 is equivalent to C = C >> 2
&= Bouting and assignment operator C & = 2 is equivalent to C = C & 2
^= Press or assignment operator C ^ = 2 is equivalent to C = C ^ 2
|= Bit or be assigned operators C | = 2 equivalent to C = C | 2

Instance

Look at the following example to learn about all the assignment operators available in the C language:

#include <stdio.h>

int 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

Miscellaneous operator

The following table lists some of the other important operators supported ? : by the C language, including sizeof(), ,

Operator describe Example
sizeof() Returns the size of the variable. SIZEOF (a) will return 4, where A is an integer.
& Returns the address of the variable. & a; the actual address of the variable will be given.
* Point a variable. * a; will point to a variable.
? : Conditional expression If the condition is true? The value is x: otherwise the value is Y.

Instance

Look at the following example to learn about all the miscellaneous operators available in the C language:

#include <stdio.h>

int main()
{
   int a = 4;
   short b;
   double c;
   int* ptr;

   /* & 和 * 运算符实例 */
   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 );
}

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

a 的值是 4
*ptr 是 4
b 的值是 30
b 的值是 20

Operator precedence in C

The operator's priority determines the combination of items in the expression. T his affects how an expression is evaluated. Some operators have a higher priority than others, for example, the multiplication operator has a higher priority than the plus-minus operator.

For example, x s 7 s 3 s 2, where x is assigned to 13 instead of 20, because the operator s has a higher priority than s, so the multiplication is calculated first, and then add 7.

The following table lists the operators by operator priority from highest to lowest, with higher priority operators appearing above the table and lower priority operators appearing below the table. In expressions, higher priority operators are evaluated first.

category Operator Conjunction
suffix () [] -> . ++ - - From left to right
One yuan + - ! ~ ++ - - (type)* & sizeof From right to left
Multiplication * / % From left to right
Decrease + - From left to right
Shift << >> From left to right
relation < <= > >= From left to right
equal == != From left to right
Bit and And & From left to right
Digital or xor ^ From left to right
Bit or OR | From left to right
Logic and AND && From left to right
Logic or OR || From left to right
condition ?: From right to left
Value = += -= *= /= %=>>= <<= &= ^= |= From right to left
comma , From left to right

Instance

Look at the following example to see the priority of operators in the C language:

#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("(a + b) * c / d 的值是 %d\n",  e );

   e = ((a + b) * c) / d;    // (30 * 15 ) / 5
   printf("((a + b) * c) / d 的值是 %d\n" ,  e );

   e = (a + b) * (c / d);   // (30) * (15/5)
   printf("(a + b) * (c / d) 的值是 %d\n",  e );

   e = a + (b * c) / d;     //  20 + (150/5)
   printf("a + (b * c) / d 的值是 %d\n" ,  e );
  
   return 0;
}

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

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