(资料图片仅供参考)

1.运算符

1.1 算术运算符

+-*/++--% -- 取余
# include int main(){  int a = 15,b = 8,c;  c = a + b;  printf("c = %d\n",c);  c = a - b;  printf("c = %d\n",c);  c = a * b;  printf("c = %d\n",c);  c = a / b;  printf("c = %d\n",c);    c = a % b;  printf("c = %d\n",c);    return 0;}
# include int main(){  float a = 15,b = 8,c;  c = a + b;  printf("c = %f\n",c);  c = a - b;  printf("c = %f\n",c);  c = a * b;  printf("c = %f\n",c);  c = a / b;  printf("c = %f\n",c);    // float是不能进行取余操作  // c = a % b;  // printf("c = %f\n",c);    return 0;}

1.2 关系运算符

> < >= <=  == !==

1.3 逻辑运算符

! -- 非&& -- 逻辑与(短路与)|| -- 逻辑或(短路或)

1.4 位运算

~ -- 按位取反#include int main(){     unsigned char x = 0x17,y;  y = ~x; // x:0001_0111   y = 1110_1000  e8  printf("%#x\n",y); // %#x -- x表示16进制打印,#表示自动补齐0x  return 0;}
& -- 按位与 unsigned char x = 0126, y = 0xac,z;z = x & y;x    01_010_110y    1010_1100x&y  00000100  0x04
| -- 按位或unsigned char x = 076,y = 0x89,z;z = x | y;x    00111110y    10001001z    10111111
^ -- 按位异或unsigned char x = 75,y = 0173,z;z = x ^ y;
移位运算运算量 运算符 表达式unsigned char a = 0xe4,b;b = a << 3;a   1110 0100b   0010 0000  -- 0x20#include int main(){  unsigned char a = 0x4,b,c,d;  b = a << 1;  C = a << 2;  d = a << 3;  printf("%#x\n",a); // a = 0000_0100 b = 0000_1000 8  printf("%#x\n",a); // 16  printf("%#x\n",a); // 32    return 0;}

1.5 复合赋值运算符

+=-=/=%=...#include int main(){  int count,sum;  count = 0;  sum = 0;  while(count++ < 20) {    sum += count;  }  printf("sum = %d\n",sum);  return 0;}

1.5 三目运算符

表达式1 ? 表达式2 : 表达式3int x = 82, y = 101;x >=y ? x+18:y-100x<(y-11):x-22:y-1#include int main(){  int x,y;  x = 70;  y = x++ > 70 ? 100 : 0;  printf("x = %d y = %d\n",x,y); // 71 0  return 0;}

1.7 逗号运算符

执行顺序是从左到右,结果由最后一个表达式决定float x = 10.5,y = 1.8,z = 0;z = (x +=5,y = x+0.2);  //z =10.7

1.8 sizeof运算符

sizeof(数据类型)sizeof(数组)sizeof(变量名)

1.9 运算符的优先级

推荐内容