int、short、long)變量 = 「一塊具名的記憶體空間」(1/2/4/8/16 位元組)。宣告時指定型別,決定大小與解讀方式。
int salary; // 宣告,配置空間,值為亂碼
salary = 10000; // 賦值
int score = 95; // 宣告並初始化(建議習慣)
salary ≠ Salary。total_pets 優於 x。int 通常 4 位元組(-21億~+21億)。其他:short (2B), long (4/8B), long long (8B)。無號版本加 unsigned 前綴。
() [] -> .++ -- (後綴) sizeof (型別)++ -- (前綴) + - (一元) ! ~* / %+ - (二元)<< >>< > <= >=== !=& (位元 AND)^ (位元 XOR)| (位元 OR)&& (邏輯 AND)|| (邏輯 OR)?: (條件)= += -= ...(右結合), (逗號)45 / 7 == 6。-45/7 == -6)。% 結果符號同被除數:45 % -7 == 3, -45 % 7 == -3。-x 翻轉符號,不改變 x 本身。-75 是編譯期常數)。int cats=2, dogs=1, ponies=1, others=46;
int total = cats + dogs + ponies + others; // 50
int cookies=5, eaten=2;
cookies = cookies - eaten; // 3
total_eaten += eaten; // 2
printf("剩 %d 塊, 共吃 %d 塊, 熱量 %d cal\n",
cookies, total_eaten, total_eaten * 125);
printf 格式字串:%d (int), %ld (long), %lld (long long)。a = b = 0 合法。本章奠定了 C 語言資料操作的基石,變量與運算是後續所有程式設計的核心,務必熟練掌握。