int和double究竟占多少個字節?c++等


       最近在看深入理解計算機系統這本書,上面提到了在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。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM