最近在看深刻理解計算機系統這本書,上面提到了在32位機器和64機器中int類型都佔用4個字節。後來,查了The C Programming language這本書,裏面有一句話是這樣的:Each compiler is free to choose appropriate sizes for its own hardware, subject only to the restriction that shorts and ints are at least 16bits, longs are at least 32bits, and short is no longer than int, which is no longer than long.意思大體是編譯器能夠根據自身硬件來選擇合適的大小,可是須要知足約束:short和int型至少爲16位,long型至少爲32位,而且short型長度不能超過int型,而int型不能超過long型。這便是說各個類型的變量長度是由編譯器來決定的,而當前主流的編譯器中通常是32位機器和64位機器中int型都是4個字節(例如,GCC)。下面列舉在GCC編譯器下32位機器和64位機器各個類型變量所佔字節數:app
C類型 | 32 | 64 |
char | 1 | 1 |
short int | 2 | 2 |
int | 4 | 4 |
long int | 4 | 8 |
long long int | 8 | 8 |
char* | 4 | 8 |
float | 4 | 4 |
double | 8 | 8 |