c++

C++ sizeof integral types


I have heard from books, presentation about minimal size for integral types in C/C++

Sizes below are in bytes:

But I cannot find those guarantees in standards. So are there any guarantees required by compilers to implement defined in standards C++98 / C++2003 / C89 / C99?


Solution

  • Because there are none. Standard defines only some basic guarantees and requirements regarding values. You can find them in this document on page 33:

    5.2.4.2.1 Sizes of integer types .

    You can be sure, that:

    sizeof(char) ≤ sizeof(short) ≤ sizeof(int) ≤ sizeof(long) ≤ sizeof(long long)
    

    and

    sizeof(float) ≤ sizeof(double) ≤ sizeof(long double)
    

    Also:

    Also, on most platforms:


    (1) long long is not a standard type. GCC and VC++ have supported it for a long time now, but official support came with C++ 11:

    C++: Fundamental Types

    long long - target type will have width of at least 64 bits. (since C++11)

    Note, that even if sizeof(char) is guaranteed to be 1, it doesn't mean, that char is 8 bit long. CHAR_BIT defines number of bits in char type. These days, almost all architectures use 8 bits per byte, but some older architectures used to have 7.