C语言教程:运算符(告诉编译器执行特定的数学或逻辑函数)

2018-10-0507:55:24编程语言入门到精通Comments2,439 views字数 6778阅读模式

C语言中,运算符是一个符号,告诉编译器执行特定的数学或逻辑函数,C语言提供丰富的内置运算符,并提供以下类型的运算符 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6372.html

  • 算术运算符
  • 关系运算符
  • 逻辑运算符
  • 按位运算符
  • 赋值运算符
  • 其它运算符

在本章中,我们将学习每个运算符的工作方式。打开Visual Studio 2017创建一个Win32 Console Application项目,名称为:c-operators文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6372.html

1.算术运算符

下表显示了C语言支持的所有算术运算符。假设变量A的值是10,变量B的值是20,那么 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6372.html

运算符描述示例
+将两个操作数相加A + B = 30
-从第一个操作数减去第二个操作数A − B = -10
*将两个操作数相乘A * B = 200
/将第一个操作数除以第二个操作数
%模数运算符和整数除法后的余数。B % A = 0
++递增运算符将整数值增加1A++ = 11
--递减运算符将整数值减1。A-- = 9

创建一个源代码文件:arithmetic_operators.c,如下代码 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6372.html

#include <stdio.h>

void 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++; 
   printf("Line 6 - Value of c is %d\n", c );

   c = a--; 
   printf("Line 7 - Value of c is %d\n", c );
}
C

执行上面示例代码,得到以下结果 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6372.html

Line 1 - Value of c is 31
Line 2 - Value of c is 11
Line 3 - Value of c is 210
Line 4 - Value of c is 2
Line 5 - Value of c is 1
Line 6 - Value of c is 21
Line 7 - Value of c is 22
请按任意键继续. . .
Shell

2.关系运算符

下表显示了C语言支持的关系运算符。假设变量A=10,变量B=20,则 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6372.html

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

创建一个源代码文件:relational_operators.c,如下代码 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6372.html

#include <stdio.h>

main() {

   int a = 21;
   int b = 10;
   int c ;

   if( a == b ) {
      printf("Line 1 - a is equal to 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" );
   }

   /* Lets change value of a and b */
   a = 5;
   b = 20;

   if ( a <= b ) {
      printf("Line 4 - a is either less than or equal to  b\n" );
   }

   if ( b >= a ) {
      printf("Line 5 - b is either greater than  or equal to b\n" );
   }
}
C

执行上面示例代码,得到以下结果 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6372.html

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 either less than or equal to  b
Line 5 - b is either greater than  or equal to b
请按任意键继续. . .
Shell

3.逻辑运算符

下表显示了C语言支持的所有逻辑运算符。 假设变量A=1,变量B=0,则 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6372.html

运算符描述示例
&&逻辑与运算符。 如果两个操作数都不为零,则条件成立。(A && B)结果为false
C语言教程:运算符(告诉编译器执行特定的数学或逻辑函数)称为逻辑或运算符。如果两个操作数中的任何一个非零,则条件成立。(AC语言教程:运算符(告诉编译器执行特定的数学或逻辑函数)B)结果为true
!称为逻辑非运算符,它用于反转其操作数的逻辑状态。如果条件为真,则逻辑NOT运算符将使其结果为false

示例:创建一个源文件:logical_operators.c,代码如下 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6372.html

#include <stdio.h>

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

   /* lets change the value of  a and b */
   a = 0;
   b = 10;

   if ( a && b ) {
      printf("Line 3 - Condition is true\n" );
   }
   else {
      printf("Line 3 - Condition is not true\n" );
   }

   if ( !(a && b) ) {
      printf("Line 4 - Condition is true\n" );
   }

}
C

执行上面代码,得到以下结果 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6372.html

Line 1 - Condition is true
Line 2 - Condition is true
Line 3 - Condition is not true
Line 4 - Condition is true
Shell

4.按位运算符

按位运算符对位进行操作,并执行逐位运算。 |^的真值表如下 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6372.html

pqp & qp/qp ^ q
00000
01011
11110
10011

假设A = 60B = 13,二进制格式如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6372.html

A = 0011 1100

B = 0000 1101

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

A&B = 0000 1100

A|B = 0011 1101

A^B = 0011 0001

~A = 1100 0011

下表列出了C语言支持的按位运算符。假设变量A=60,变量B=13,则 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6372.html

运算符描述示例
&如果二进制AND运算符存在于两个操作数中,则二进制AND运算符将对结果复制一位。(A&B)= 12,即0000 1100
C语言教程:运算符(告诉编译器执行特定的数学或逻辑函数)二进制OR运算符如果存在于任一操作数中,则复制一位。(AC语言教程:运算符(告诉编译器执行特定的数学或逻辑函数)B) = 61, 即 0011 1101
^二进制XOR操作符复制该位,如果它设置在一个操作数中,而不是两者。(A ^ B) = 49, 即, 0011 0001
~二进制补码运算符是一元的,具有“翻转”位的作用。(~A)= -61,即 1100 0011的补码形式。
<<二进制左移操作符,左操作数值左移由右操作数指定的位数。A << 2 = 240 即, 1111 0000
>>二进制右移操作符,左操作数值被右操作数指定的位移动。A >> 2 = 15 即,0000 1111

示例: 创建一个源代码文件:bitwise_operators.c,代码如下所示 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6372.html

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

执行上面代码,得到以下结果 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6372.html

Line 1 - Value of c is 12
Line 2 - Value of c is 61
Line 3 - Value of c is 49
Line 4 - Value of c is -61
Line 5 - Value of c is 240
Line 6 - Value of c is 15
请按任意键继续. . .
Shell

5.赋值运算符

下表列出了C语言支持的赋值运算符 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6372.html

运算符描述示例
=简单赋值运算符,将右侧操作数的值分配给左侧操作数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语言教程:运算符(告诉编译器执行特定的数学或逻辑函数)按位包含OR和赋值运算符。C语言教程:运算符(告诉编译器执行特定的数学或逻辑函数)

示例: 创建一个源文件:assignment_operators.c ,其代码如下 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6372.html

#include <stdio.h>

void main() {

   int a = 21;
   int c ;

   c =  a;
   printf("Line 1 - =  Operator Example, Value of c = %d\n", c );

   c +=  a;
   printf("Line 2 - += Operator Example, Value of c = %d\n", c );

   c -=  a;
   printf("Line 3 - -= Operator Example, Value of c = %d\n", c );

   c *=  a;
   printf("Line 4 - *= Operator Example, Value of c = %d\n", c );

   c /=  a;
   printf("Line 5 - /= Operator Example, Value of c = %d\n", c );

   c  = 200;
   c %=  a;
   printf("Line 6 - %= Operator Example, Value of c = %d\n", c );

   c <<=  2;
   printf("Line 7 - <<= Operator Example, Value of c = %d\n", c );

   c >>=  2;
   printf("Line 8 - >>= Operator Example, Value of c = %d\n", c );

   c &=  2;
   printf("Line 9 - &= Operator Example, Value of c = %d\n", c );

   c ^=  2;
   printf("Line 10 - ^= Operator Example, Value of c = %d\n", c );

   c |=  2;
   printf("Line 11 - |= Operator Example, Value of c = %d\n", c );

}
C

执行上面代码,得到以下结果 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6372.html

Line 1 - =  Operator Example, Value of c = 21
Line 2 - += Operator Example, Value of c = 42
Line 3 - -= Operator Example, Value of c = 21
Line 4 - *= Operator Example, Value of c = 441
Line 5 - /= Operator Example, Value of c = 21
Line 6 - = Operator Example, Value of c = 11
Line 7 - <<= Operator Example, Value of c = 44
Line 8 - >>= Operator Example, Value of c = 11
Line 9 - &= Operator Example, Value of c = 2
Line 10 - ^= Operator Example, Value of c = 0
Line 11 - |= Operator Example, Value of c = 2
请按任意键继续. . .
Shell

6.其他操作符:sizeof和三元运算符

除了上面讨论的运算符,还有一些其他重要的运算符,包括sizeof? :也被C语言所支持。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6372.html

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

示例: 创建一个源文件:sizeof_operator.c ,其代码如下 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6372.html

#include <stdio.h>

void main() {

    int a = 4;
    short b;
    double c;
    int* ptr;

    /* example of sizeof operator */
    printf("Line 1 - Size of variable a = %d\n", sizeof(a));
    printf("Line 2 - Size of variable b = %d\n", sizeof(b));
    printf("Line 3 - Size of variable c= %d\n", sizeof(c));

    /* example of & and * operators */
    ptr = &a;    /* 'ptr' now contains the address of 'a'*/
    printf("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("Value of b is %d\n", b);

    b = (a == 10) ? 20 : 30;
    printf("Value of b is %d\n", b);
}
C

执行上面代码,得到以下结果 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6372.html

Line 1 - Size of variable a = 4
Line 2 - Size of variable b = 2
Line 3 - Size of variable c= 8
value of a is  4
*ptr is 4.
Value of b is 30
Value of b is 20
请按任意键继续. . .
Shell

7.运算符优先级

运算符优先级决定表达式中术语的分组,并决定如何评估计算表达式。 某些运算符的优先级高于其他运营商; 例如,乘法运算符的优先级高于加法运算符,则先要执行乘法运算符的运算。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6372.html

让我们通过下面的例子了解优先级:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6372.html

int value = 10 + 20 * 10;
C

value变量计算结果为:210,因为*(乘法运算符)的优先级比+(加法运算符)高,所以在+(加法运算符)之前进行求值。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6372.html

C语言运算符的优先级和关联性如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6372.html

分类运算符关联性
后缀() [] -> . ++ - -左到右
一元+ - ! ~ ++ - - (type)* & sizeof右到左
乘法* / %左到右
加法+ -左到右
位移<< >>左到右
关系< <= > >=左到右
等于== !=左到右
按位与&左到右
位异或^左到右
按位或/左到右
逻辑与&&左到右
逻辑或//左到右
条件?:右到左
赋值= += -= *= /= %=>>= <<= &= ^= /=右到左
逗号,左到右

示例: 创建一个源文件:operators_precedence.c ,其代码如下 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6372.html

#include <stdio.h>

void main() {

    int a = 20;
    int b = 10;
    int c = 15;
    int d = 5;
    int e;

    e = (a + b) * c / d;      // ( 30 * 15 ) / 5
    printf("Value of (a + b) * c / d is : %d\n", e);

    e = ((a + b) * c) / d;    // (30 * 15 ) / 5
    printf("Value of ((a + b) * c) / d is  : %d\n", e);

    e = (a + b) * (c / d);   // (30) * (15/5)
    printf("Value of (a + b) * (c / d) is  : %d\n", e);

    e = a + (b * c) / d;     //  20 + (150/5)
    printf("Value of a + (b * c) / d is  : %d\n", e);

    return 0;
}
C

执行上面代码,得到以下结果 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6372.html

Value of (a + b) * c / d is : 90
Value of ((a + b) * c) / d is  : 90
Value of (a + b) * (c / d) is  : 90
Value of a + (b * c) / d is  : 50
请按任意键继续. . .
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6372.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/ymba/6372.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定