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。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章