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

前端 未结 24 2210
无人及你
无人及你 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:52

    The C++ Standard says it like this:

    3.9.1, §2:

    There are five signed integer types : "signed char", "short int", "int", "long int", and "long long int". In this list, each type provides at least as much storage as those preceding it in the list. Plain ints have the natural size suggested by the architecture of the execution environment (44); the other signed integer types are provided to meet special needs.

    (44) that is, large enough to contain any value in the range of INT_MIN and INT_MAX, as defined in the header <climits>.

    The conclusion: It depends on which architecture you're working on. Any other assumption is false.

    0 讨论(0)
  • 2020-11-21 04:53

    For 32-bit systems, the 'de facto' standard is ILP32 — that is, int, long and pointer are all 32-bit quantities.

    For 64-bit systems, the primary Unix 'de facto' standard is LP64 — long and pointer are 64-bit (but int is 32-bit). The Windows 64-bit standard is LLP64 — long long and pointer are 64-bit (but long and int are both 32-bit).

    At one time, some Unix systems used an ILP64 organization.

    None of these de facto standards is legislated by the C standard (ISO/IEC 9899:1999), but all are permitted by it.

    And, by definition, sizeof(char) is 1, notwithstanding the test in the Perl configure script.

    Note that there were machines (Crays) where CHAR_BIT was much larger than 8. That meant, IIRC, that sizeof(int) was also 1, because both char and int were 32-bit.

    0 讨论(0)
  • 2020-11-21 04:53

    We are allowed to define a synonym for the type so we can create our own "standard".

    On a machine in which sizeof(int) == 4, we can define:

    typedef int int32;
    
    int32 i;
    int32 j;
    ...
    

    So when we transfer the code to a different machine where actually the size of long int is 4, we can just redefine the single occurrence of int.

    typedef long int int32;
    
    int32 i;
    int32 j;
    ...
    
    0 讨论(0)
  • 2020-11-21 04:53

    As mentioned the size should reflect the current architecture. You could take a peak around in limits.h if you want to see how your current compiler is handling things.

    0 讨论(0)
  • 2020-11-21 04:53

    You can use variables provided by libraries such as OpenGL, Qt, etc.

    For example, Qt provides qint8 (guaranteed to be 8-bit on all platforms supported by Qt), qint16, qint32, qint64, quint8, quint16, quint32, quint64, etc.

    0 讨论(0)
  • 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 <inttypes.h>, 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.

    0 讨论(0)
提交回复
热议问题