size guarantee for integral/arithmetic types in C and C++

前端 未结 7 2148
难免孤独
难免孤独 2020-12-15 00:49

I know that the C++ standard explicitly guarantees the size of only char, signed char and unsigned char. Also it gives guarantees that

7条回答
  •  眼角桃花
    2020-12-15 01:06

    Don't know about C++. In C you have

    
                                      Annex E
                                  (informative)
    
    
                              Implementation limits
    
           [#1]  The contents of the header  are given below,
           in alphabetical order.  The minimum magnitudes  shown  shall
           be  replaced  by  implementation-defined magnitudes with the
           same sign.  The values shall  all  be  constant  expressions
           suitable  for  use  in  #if  preprocessing  directives.  The
           components are described further in 5.2.4.2.1.
    
                   #define CHAR_BIT                         8
                   #define CHAR_MAX    UCHAR_MAX or SCHAR_MAX
                   #define CHAR_MIN            0 or SCHAR_MIN
                   #define INT_MAX                     +32767
                   #define INT_MIN                     -32767
                   #define LONG_MAX               +2147483647
                   #define LONG_MIN               -2147483647
                   #define LLONG_MAX     +9223372036854775807
                   #define LLONG_MIN     -9223372036854775807
                   #define MB_LEN_MAX                       1
                   #define SCHAR_MAX                     +127
                   #define SCHAR_MIN                     -127
                   #define SHRT_MAX                    +32767
                   #define SHRT_MIN                    -32767
                   #define UCHAR_MAX                      255
                   #define USHRT_MAX                    65535
                   #define UINT_MAX                     65535
                   #define ULONG_MAX               4294967295
                   #define ULLONG_MAX    18446744073709551615
    

    So char <= short <= int <= long <= long long

    and

    CHAR_BIT * sizeof (char) >= 8
    CHAR_BIT * sizeof (short) >= 16
    CHAR_BIT * size of (int) >= 16
    CHAR_BIT * sizeof (long) >= 32
    CHAR_BIT * sizeof (long long) >= 64

提交回复
热议问题