分类 | 运算符符号 | 描述 | 示例代码 | 优先级 |
---|---|---|---|---|
算术运算符 | + - * / % |
加减乘除取余 | 5 + 3 * 2 → 11 |
4-3 |
赋值运算符 | = += -= *= /= %= |
基本及复合赋值 | coins += 5 → coins=15 |
2 |
关系运算符 | > < == != >= <= |
比较大小关系 | 5 > 3 → 1 |
6-7 |
逻辑运算符 | && ` |
!` |
逻辑与/或/非 | |
位运算符 | & ` |
^` `~` `<<` `>>` |
二进制位操作 | 0b1010 & 0b0101 → 0b0000 |
其他运算符 | () [] . -> |
括号/数组/结构体成员访问 | arr[0] ptr->value |
1 |
++ -- |
自增自减 | i++ → i=i+1 |
2 | |
sizeof |
计算数据类型大小 | sizeof(int) → 4 |
2 | |
? : |
三目条件运算符 | a>b ? a : b |
13 | |
, |
逗号运算符 | int x=(5,6,7) → x=7 |
14 | |
& * |
取地址/间接访问 | int *p=&x; *p=5 |
2 | |
(类型) |
强制类型转换 | (float)total / quantity |
2 |
+
int apples = 3, oranges = 5;
printf("水果总数:%d", apples + oranges); // 8
-
int birthYear = 2000, currentYear = 2025;
printf("年龄:%d", currentYear - birthYear); // 25
*
float radius = 3.5;
printf("圆面积:%.2f", 3.14 * radius * radius); // 38.47
/
int slices = 7, people = 3;
printf("每人分到:%d片", slices / people); // 2
%
int number = 13;
printf("%s", number % 2 ? "奇数" : "偶数"); // 奇数
=
int score = 90; // 把90赋值给score
+=
变量 = 变量 + 值
int coins = 10;
coins += 5; // 等价于 coins = coins + 5 → 15
-=
变量 = 变量 - 值
int balance = 100;
balance -= 30; // 等价于 balance = balance - 30 → 70
*=
变量 = 变量 * 值
int speed = 5;
speed *= 2; // 等价于 speed = speed * 2 → 10
/=
变量 = 变量 / 值
int slices = 12;
slices /= 3; // 等价于 slices = slices / 3 → 4
%=
变量 = 变量 % 值
int remainder = 10;
remainder %= 3; // 等价于 remainder = remainder % 3 → 1
运算符 | 含义 | 示例 |
---|---|---|
> |
大于 | 5 > 3 → 1 |
< |
小于 | 2 < 4 → 1 |
== |
等于 | 3 == 3 → 1 |
!= |
不等于 | 5 != 6 → 1 |
>= |
大于等于 | 5 >= 5 → 1 |
<= |
小于等于 | 4 <= 3 → 0 |
#include
int main() {
int a = 5, b = 3;
// 大于
printf("5 > 3 → %d\n", a > b); // 输出: 1
// 小于等于
printf("3 <= 5 → %d\n", 3 <= 5); // 输出: 1
// 不等于
printf("a != b → %d\n", a != b); // 输出: 1
return 0;
}
&&
#include
int main() {
int age = 20;
int score = 70;
if (age >= 18 && score > 60) {
printf("合格");
} else {
printf("不合格");
}
return 0;
}
||
#include
int main() {
int isVIP = 1;
int points = 800;
if (isVIP || points > 1000) {
printf("可兑换");
} else {
printf("不可兑换");
}
return 0;
}
!
#include
#include
// 模拟显示登录页面的函数
void showLoginPage() {
printf("显示登录页面\n");
}
int main() {
bool isLoggedIn = false;
if (!isLoggedIn) {
showLoginPage();
}
return 0;
}
&
#include
int main() {
unsigned char flag = 0b1010;
flag &= 0b0101;
printf("按位与运算结果: 0b%04b\n", flag);
return 0;
}
|
#include
int main() {
unsigned char led = 0b0000;
led |= 0b0001;
printf("按位或运算后 led 的值: 0b%04b\n", led);
return 0;
}
^
#include
int main() {
unsigned char num1 = 0b1010;
unsigned char num2 = 0b0101;
unsigned char result = num1 ^ num2;
printf("按位异或运算结果: 0b%04b\n", result);
return 0;
}
~
#include
int main() {
unsigned char data = 0b1100;
unsigned char result = ~data;
printf("按位取反运算结果: 0b%04b\n", result);
return 0;
}
<<
#include
int main() {
int speed = 5;
speed <<= 1;
printf("左移运算后 speed 的值: %d\n", speed);
return 0;
}
>>
#include
int main() {
int population = 100;
population >>= 2;
printf("右移运算后 population 的值: %d\n", population);
return 0;
}
int x = (5, 6, 7);
结果x=7max = (a > b) ? a : b;
float price = (float)total / quantity;
程序员的老婆让他去买酱油:
“运算符是代码的标点符号,用好了能写出优美的诗,用错了就是乱码的诅咒!”