Latest web development tutorials

C Operators

Operator is a symbol to tell the compiler to perform 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

This chapter describes each of arithmetic operators, relational operators, logical operators, bitwise operators, assignment operators, and other operators.

Arithmetic operators

The following table shows the C language support for all arithmetic operators.A value of 10 is assumed variable, the variable Bis 20, then:

运算符描述实例
+把两个操作数相加 A + B 将得到 30
-从第一个操作数中减去第二个操作数 A - B 将得到 -10
*把两个操作数相乘 A * B 将得到 200
/分子除以分母 B / A 将得到 2
%取模运算符,整除后的余数 B % A 将得到 0
++自增运算符,整数值增加 1 A++ 将得到 11
--自减运算符,整数值减少 1 A-- 将得到 9

Examples

Consider the following examples to learn C language all the available arithmetic operators:

#include <stdio.h>

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++; 
   printf("Line 6 - c 的值是 %d\n", c );
   c = a--; 
   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

Relational Operators

The following table shows the C language supported by all relational operators.A value of 10 is assumed variable, the variable Bis 20, then:

运算符描述实例
==检查两个操作数的值是否相等,如果相等则条件为真。 (A == B) 不为真。
!=检查两个操作数的值是否相等,如果不相等则条件为真。 (A != B) 为真。
>检查左操作数的值是否大于右操作数的值,如果是则条件为真。 (A > B) 不为真。
<检查左操作数的值是否小于右操作数的值,如果是则条件为真。 (A < B) 为真。
>=检查左操作数的值是否大于或等于右操作数的值,如果是则条件为真。 (A >= B) 不为真。
<=检查左操作数的值是否小于或等于右操作数的值,如果是则条件为真。 (A <= B) 为真。

Examples

Consider the following examples to learn C language relational operators are available:

#include <stdio.h>

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

Logical Operators

The following table shows the relationship between the C language supported by all logical operators. Suppose the variableA is 1, the variable Bis 0, then:

运算符描述实例
&&称为逻辑与运算符。如果两个操作数都非零,则条件为真。 (A && B) 为假。
||称为逻辑或运算符。如果两个操作数中有任意一个非零,则条件为真。 (A || B) 为真。
!称为逻辑非运算符。用来逆转操作数的逻辑状态。如果条件为真则逻辑非运算符将使其为假。 !(A && B) 为真。

Examples

Consider the following examples to learn C language all available logical operators:

#include <stdio.h>

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 - 条件为真

Bitwise Operators

Bitwise operators acting on the bit, and bit by bit operation. &, | And ^ truth table is as follows:

pqp & qp | qp ^ q
00000
01011
11110
10011

Suppose if A = 60, and B = 13, and now, in binary form, 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 C language support bitwise operators. Suppose the variableA is 60, the value of the variable B13, then:

运算符描述实例
&如果同时存在于两个操作数中,二进制 AND 运算符复制一位到结果中。 (A & B) 将得到 12,即为 0000 1100
|如果存在于任一操作数中,二进制 OR 运算符复制一位到结果中。 (A | B) 将得到 61,即为 0011 1101
^如果存在于其中一个操作数中但不同时存在于两个操作数中,二进制异或运算符复制一位到结果中。 (A ^ B) 将得到 49,即为 0011 0001
~二进制补码运算符是一元运算符,具有"翻转"位效果。 (~A ) 将得到 -61,即为 1100 0011,2 的补码形式,带符号的二进制数。
<<二进制左移运算符。左操作数的值向左移动右操作数指定的位数。 A << 2 将得到 240,即为 1111 0000
>>二进制右移运算符。左操作数的值向右移动右操作数指定的位数。 A >> 2 将得到 15,即为 0000 1111

Examples

Consider the following examples to learn C language bitwise operators are available:

#include <stdio.h>

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

Assignment Operators

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

运算符描述实例
=简单的赋值运算符,把右边操作数的值赋给左边操作数 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
|=按位或且赋值运算符 C |= 2 等同于 C = C | 2

Examples

Consider the following examples to learn C language all available assignment operators:

#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

Miscellaneous Operators & map; sizeof & three yuan

The following table lists the C language support for some other important operators, includingsizeof and?:.

运算符描述实例
sizeof()返回变量的大小。sizeof(a) 将返回 4,其中 a 是整数。
&返回变量的地址。&a; 将给出变量的实际地址。
*指向一个变量。*a; 将指向一个变量。
? :条件表达式如果条件为真 ? 则值为 X : 否则值为 Y

Examples

Consider the following examples to learn C language all available miscellaneous operators:

#include <stdio.h>

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

   /* sizeof 运算符实例 */
   printf("Line 1 - 变量 a 的大小 = %d\n", sizeof(a) );
   printf("Line 2 - 变量 b 的大小 = %d\n", sizeof(b) );
   printf("Line 3 - 变量 c 的大小 = %d\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 );
}

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

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

The C operator precedence

Operator precedence determine a combined term of an expression. This affects how an expression calculation. Some operators have a higher priority than other operators, such as addition and subtraction, multiplication and division operator than the operator a higher priority.

For example, x = 7 + 3 * 2, where, x is assigned to 13 instead of 20, because the * operator has higher precedence than +, so first calculate the multiplication 3 * 2, then add 7.

The following table will be listed in order of operator precedence for each operator, having the above higher priority operators appear in the table, with the following lower priority operators appear in the table. In the expression, higher priority operators priority is calculated.

类别  运算符 结合性 
后缀 () [] -> . ++ - -   从左到右 
一元  + - ! ~ ++ - - (type)* & sizeof  从右到左 
乘除  * / % 从左到右 
加减 + -  从左到右 
移位  << >>  从左到右 
关系 < <= > >=  从左到右 
相等  == !=  从左到右 
位与 AND  从左到右 
位异或 XOR  从左到右 
位或 OR  从左到右 
逻辑与 AND &&  从左到右 
逻辑或 OR  ||  从左到右 
条件 ?:  从右到左 
赋值  = += -= *= /= %=>>= <<= &= ^= |= 从右到左 
逗号  从左到右 

Examples

Consider the following examples to learn C language operator precedence:

#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 / 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