每日快報!03-運算符

來源:博客園時間:2023-05-17 06:25:53


(資料圖片僅供參考)

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 逗號運算符

執(zhí)行順序是從左到右,結果由最后一個表達式決定float x = 10.5,y = 1.8,z = 0;z = (x +=5,y = x+0.2);  //z =10.7

1.8 sizeof運算符

sizeof(數(shù)據類型)sizeof(數(shù)組)sizeof(變量名)

1.9 運算符的優(yōu)先級

標簽:

責任編輯:FD31
上一篇:nh4cl的電子式怎么寫_nh4cl的電子式|重點聚焦
下一篇:最后一頁

精彩圖集(熱圖)

最近更新

信用中國

  • 信用信息
  • 行政許可和行政處罰
  • 網站文章