C语言运算符与流程控制详解
运算符概述
运算符是告诉编译器执行特定数学或逻辑功能的符号。C语言包含丰富的内置运算符,主要分为以下几类:
算术运算符
假设变量A的值为10,变量B的值为20:
操作符 | 描述 | 例子 |
---|---|---|
+ | 加两个操作数 | A + B = 30 |
- | 从第一个减去第二个操作数 | A − B = -10 |
* | 将两个操作数相乘 | A * B = 200 |
/ | 将分子除以除分母 | B / A = 2 |
% | 模运算符和整数除法后的余数 | B % A = 0 |
++ | 增量运算符将整数值增加一 | A++ = 11 |
-- | 减量运算符将整数值减一 | A-- = 9 |
c
#include <stdio.h>int 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);return 0; }
关系运算符
假设变量A的值为10,变量B的值为20:
操作符 | 描述 | 例子 |
---|---|---|
== | 检查两个操作数的值是否相等 | (A == B) 为 false |
!= | 检查两个操作数的值是否相等 | (A != B) 为 true |
> | 检查左操作数的值是否大于右操作数 | (A > B) 为 false |
< | 检查左操作数的值是否小于右操作数 | (A < B) 为 true |
>= | 检查左操作数的值是否大于或等于右操作数 | (A >= B) 为 false |
<= | 检查左操作数的值是否小于或等于右操作数 | (A <= B) 为 true |
c
#include <stdio.h>int 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");}/* 改变a和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 a\n");}return 0; }
逻辑运算符
假设变量A持有1,变量B持有0:
操作符 | 描述 | 例子 |
---|---|---|
&& | 逻辑与运算符。如果两个操作数都不为零,则条件为true | (A && B) 为 false |
|| | 逻辑或运算符。如果两个操作数中的任何一个不为零,则条件为true | (A || B) 为 true |
! | 逻辑非运算符。用于反转其操作数的逻辑状态 | !(A && B) 为 true |
c
#include <stdio.h>int 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");}/* 改变a和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");}return 0; }
按位运算符
按位运算符对位进行运算并执行逐位操作。假设A = 60,B = 13:
text
A = 0011 1100 B = 0000 1101 ----------------- A & B = 0000 1100 A | B = 0011 1101 A ^ B = 0011 0001 ~A = 1100 0011
操作符 | 描述 | 例子 |
---|---|---|
& | 二进制AND运算符 | (A & B) = 12,即0000 1100 |
| | 二进制OR运算符 | (A | B) = 61,即0011 1101 |
^ | 二进制XOR运算符 | (A ^ B) = 49,即0011 0001 |
~ | 二进制补码运算符 | (~A) = -61,即1100 0011 |
<< | 二进制左移运算符 | A << 2 = 240,即1111 0000 |
>> | 二进制右移运算符 | A >> 2 = 15,即0000 1111 |
c
#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 - 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);return 0; }
赋值运算符
操作符 | 描述 | 例子 |
---|---|---|
= | 简单的赋值运算符 | C = A + B |
+= | 加和赋值运算符 | 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 |
c
#include <stdio.h>int 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);return 0; }
杂项运算符
操作符 | 描述 | 例子 |
---|---|---|
sizeof() | 返回变量的大小 | sizeof(a) |
& | 返回变量的地址 | &a |
* | 指向变量的指针 | *a |
?: | 条件表达式 | 条件 ? 值1 : 值2 |
c
#include <stdio.h>int main() {int a = 4;short b;double c;int* ptr;/* sizeof运算符示例 */printf("Line 1 - Size of variable a = %lu\n", sizeof(a));printf("Line 2 - Size of variable b = %lu\n", sizeof(b));printf("Line 3 - Size of variable c = %lu\n", sizeof(c));/* & 和 * 运算符示例 */ptr = &a; /* 'ptr' 现在包含 'a' 的地址 */printf("value of a is %d\n", a);printf("*ptr is %d.\n", *ptr);/* 三元运算符示例 */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);return 0; }
运算符优先级
运算符优先级确定表达式中术语的分组,并确定如何评估表达式。
类别 | 操作符 | 关联性 |
---|---|---|
后缀 | () [] -> . ++ -- | 左到右 |
一元 | + - ! ~ ++ -- (type) * & sizeof | 右到左 |
乘性 | * / % | 左到右 |
加减 | + - | 左到右 |
位移 | << >> | 左到右 |
关系型 | < <= > >= | 左到右 |
相等 | == != | 左到右 |
按位与 | & | 左到右 |
按位异或 | ^ | 左到右 |
按位或 | | | 左到右 |
逻辑与 | && | 左到右 |
逻辑或 | || | 左到右 |
条件 | ?: | 右到左 |
赋值 | = += -= *= /= %= >>= <<= &= ^= |= | 右到左 |
逗号 | , | 左到右 |
c
#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) / 5printf("Value of (a + b) * c / d is : %d\n", e);e = ((a + b) * c) / d; // (30 * 15) / 5printf("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编程语言将任何非零和非null值假定为true,如果为零或null,则假定为false值。
if 语句
c
#include <stdio.h>int main() {int a = 10;if(a < 20) {printf("a is less than 20\n");}printf("value of a is : %d\n", a);return 0; }
if...else 语句
c
#include <stdio.h>int main() {int a = 100;if(a < 20) {printf("a is less than 20\n");} else {printf("a is not less than 20\n");}printf("value of a is : %d\n", a);return 0; }
嵌套 if 语句
c
#include <stdio.h>int main() {int a = 100;int b = 200;if(a == 100) {if(b == 200) {printf("Value of a is 100 and b is 200\n");}}printf("Exact value of a is : %d\n", a);printf("Exact value of b is : %d\n", b);return 0; }
switch 语句
c
#include <stdio.h>int main() {char grade = 'B';switch(grade) {case 'A' :printf("Excellent!\n");break;case 'B' :case 'C' :printf("Well done\n");break;case 'D' :printf("You passed\n");break;case 'F' :printf("Better try again\n");break;default :printf("Invalid grade\n");}printf("Your grade is %c\n", grade);return 0; }
嵌套 switch 语句
c
#include <stdio.h>int main() {int a = 100;int b = 200;switch(a) {case 100: printf("This is part of outer switch\n");switch(b) {case 200:printf("This is part of inner switch\n");}}printf("Exact value of a is : %d\n", a);printf("Exact value of b is : %d\n", b);return 0; }
条件运算符 ?:
条件运算符 ?:
可以替代 if...else
语句,具有以下形式:
c
Exp1 ? Exp2 : Exp3;
其中:
如果
Exp1
为 true,则计算Exp2
并成为整个表达式的值如果
Exp1
为 false,则计算Exp3
并成为表达式的值
C语言提供了丰富的运算符和灵活的控制结构,使得程序员能够编写出高效且功能强大的程序。理解运算符的优先级和结合性对于编写正确的表达式至关重要,而掌握各种决策结构则能帮助构建复杂的程序逻辑。