C 語言基本整型資料類型包括字元、短整型、整型、長整型,皆分為有號與無號兩種版本。
整型值大小規則:長整型至少與整型同長,整型至少與短整型同長。

long、unsigned long、指標為機器全字長,其大小與 CPU 位數一致。
#include <stdio.h>
int main() {
printf("the char range is %-20hhd~%20hhu, size is %u\n", 0x80, 0x7f, sizeof(char));
printf("the unsigned char range is %-20hhu~%20hhu, size is %u\n", 0x0, 0xff, sizeof(unsigned char));
printf("the short range is %-20hd~%20hu, size is %u\n", 0x8000, 0x7fff, sizeof(short));
printf("the unsigned short range is %-20hu~%20hu, size is %u\n", 0x0, 0xffff, sizeof(unsigned short));
printf("the int range is %-20d~%20u, size is %u\n", 0x80000000, 0x7fffffff, sizeof(int));
printf("the unsigned int range is %-20u~%20u, size is %u\n", 0x0, 0xffffffff, sizeof(unsigned int));
printf("the long range is %-20ld~%20lu, size is %u\n", 0x80000000, 0x7fffffff, sizeof(long));
printf("the unsigned long range is %-20lu~%20lu, size is %u\n", 0x0, 0xffffffff, sizeof(unsigned long));
printf("the llong range is %-20lld~%20llu, size is %u\n", 0x8000000000000000, 0x7fffffffffffffff, sizeof(long long));
printf("the unsigned llong range is %-20lld~%20llu, size is %u\n", 0x0ll, 0xffffffffffffffff, sizeof(unsigned long long));
printf("default constant size is %u %u %u\n", sizeof('b'), sizeof(0x0), sizeof(0x0ll));
}
the char range is -128 ~ 127, size is 1 the unsigned char range is 0 ~ 255, size is 1 the short range is -32768 ~ 32767, size is 2 the unsigned short range is 0 ~ 65535, size is 2 the int range is -2147483648 ~ 2147483647, size is 4 the unsigned int range is 0 ~ 4294967295, size is 4 the long range is -2147483648 ~ 2147483647, size is 4 the unsigned long range is 0 ~ 4294967295, size is 4 the llong range is -9223372036854775808 ~ 9223372036854775807, size is 8 the unsigned llong range is 0 ~ 18446744073709551615, size is 8 default constant size is 4 4 8
L 或 l:解釋為 long 整型值。U 或 u:指定為 unsigned 整型值。unsigned long 整型值。int,不可添加 unsigned 或 long 後綴。C 語言一般預設整型常數(字元/整型常數)類型為 int。
%llu,printf 認為輸出值大小為 8 位元組,若對應參數為 0x0(4 位元組),會讀取 8 位元組導致亂碼。printf 輸出整型變數時,打印有號或無號取決於格式字元,與實際傳入參數類型及符號無關。int i = 0x80001010;
printf("%d\n", i); // 輸出 -2147479536
printf("%u\n", i); // 輸出 2147487760
printf("%hu\n", i); // 輸出 4112(高兩位截斷,實際為 0x1010)
printf("%hhu\n", i); // 輸出 16(高三位截斷,實際為 0x10)
結論:使用 printf 打印變數的大小與符號與變數本身無關,即使強制轉型也無效,必須選擇對應正確的格式字元。