1. 程式人生 > >int型別究竟佔幾個位元組

int型別究竟佔幾個位元組

最近在看深入理解計算機系統這本書,上面提到了在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位機器各個型別變數所佔位元組數:

     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

        需要說明一下的是指標型別儲存的是所指向變數的地址,所以32位機器只需要32bit,而64位機器需要64bit。