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

2023年 7月 14日 18.5k 0

C语言中,运算符是一个符号,告诉编译器执行特定的数学或逻辑函数,C语言提供丰富的内置运算符,并提供以下类型的运算符 -

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

在本章中,我们将学习每个运算符的工作方式。打开Visual Studio 2017创建一个Win32 Console Application项目,名称为:c-operators 。

1.算术运算符

下表显示了C语言支持的所有算术运算符。假设变量A的值是10,变量B的值是20,那么 -

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

创建一个源代码文件:arithmetic_operators.c,如下代码 -

#include 

void main() {

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

   c = a + b;
   printf("Line 1 - Value of c is %dn", c );

   c = a - b;
   printf("Line 2 - Value of c is %dn", c );

   c = a * b;
   printf("Line 3 - Value of c is %dn", c );

   c = a / b;
   printf("Line 4 - Value of c is %dn", c );

   c = a % b;
   printf("Line 5 - Value of c is %dn", c );

   c = a++; 
   printf("Line 6 - Value of c is %dn", c );

   c = a--; 
   printf("Line 7 - Value of c is %dn", c );
}

C

执行上面示例代码,得到以下结果 -

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,则 -

运算符 描述 示例
== 检查两个操作数的值是否相等。 如果相等,则条件成立。 (A == B)结果为false
!= 检查两个操作数的值是否相等。 如果值不相等,则条件成立。 (A != B) 结果为true
> 检查左操作数的值是否大于右操作数的值。 如果是,则条件成立。 (A > B) 结果为false
= 检查左操作数的值是否大于等于右操作数的值。 如果是,则条件成立。 (A >= B) 结果为false
2
&= 按位与赋值运算符 C &= 2等价于C = C & 2
^= 按位异或运算符和赋值运算符。 C ^= 2等价于C = C ^ 2
C语言教程:运算符(告诉编译器执行特定的数学或逻辑函数) 按位包含OR和赋值运算符。 C语言教程:运算符(告诉编译器执行特定的数学或逻辑函数)

示例: 创建一个源文件:assignment_operators.c ,其代码如下 -

#include 

void main() {

   int a = 21;
   int c ;

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

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

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

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

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

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

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

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

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

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

}

C

执行上面代码,得到以下结果 -

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 = 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语言所支持。

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

示例: 创建一个源文件:sizeof_operator.c ,其代码如下 -

#include 

void main() {

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

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

    /* example of & and * operators */
    ptr = &a;    /* 'ptr' now contains the address of 'a'*/
    printf("value of a is  %dn", a);
    printf("*ptr is %d.n", *ptr);

    /* example of ternary operator */
    a = 10;
    b = (a == 1) ? 20 : 30;
    printf("Value of b is %dn", b);

    b = (a == 10) ? 20 : 30;
    printf("Value of b is %dn", b);
}

C

执行上面代码,得到以下结果 -

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.运算符优先级

运算符优先级决定表达式中术语的分组,并决定如何评估计算表达式。 某些运算符的优先级高于其他运营商; 例如,乘法运算符的优先级高于加法运算符,则先要执行乘法运算符的运算。

让我们通过下面的例子了解优先级:

int value = 10 + 20 * 10;

C

value变量计算结果为:210,因为*(乘法运算符)的优先级比+(加法运算符)高,所以在+(加法运算符)之前进行求值。

C语言运算符的优先级和关联性如下:

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

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论