What does the C++ standard state the size of int, long type to be?

前端 未结 24 2911
无人及你
无人及你 2020-11-21 04:42

I\'m looking for detailed information regarding the size of basic C++ types. I know that it depends on the architecture (16 bits, 32 bits, 64 bits) and the compiler.

24条回答
  •  佛祖请我去吃肉
    2020-11-21 04:54

    There is standard.

    C90 standard requires that

    sizeof(short) <= sizeof(int) <= sizeof(long)
    

    C99 standard requires that

    sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long)
    

    Here is the C99 specifications. Page 22 details sizes of different integral types.

    Here is the int type sizes (bits) for Windows platforms:

    Type           C99 Minimum     Windows 32bit
    char           8               8
    short          16              16
    int            16              32
    long           32              32
    long long      64              64
    

    If you are concerned with portability, or you want the name of the type reflects the size, you can look at the header , where the following macros are available:

    int8_t
    int16_t
    int32_t
    int64_t
    

    int8_t is guaranteed to be 8 bits, and int16_t is guaranteed to be 16 bits, etc.

提交回复
热议问题