C和C++中的积分/算术类型的大小保证

Arm*_*yan 9 c c++ sizeof primitive-types

我知道C++标准明确保证了大小char,signed charunsigned char.此外,它提供了保证,比方说,short至少是一样大的char,int大如short等,但有关的,比如说绝对值没有明确的保证,sizeof(int).这是我头脑中的信息,我和它幸福地生活在一起.然而,不久之前,我在SO中找到了一条评论(找不到它),在C long中保证至少有4个字节,而且这个要求是由C++"继承"的.是这样的吗?如果是这样,我们对C++中算术类型的大小有什么其他的隐含保证?请注意,我对这个问题中不同平台的实际保证完全不感兴趣,只是理论上的 那些.

Ste*_*sop 13

18.2.2保证<climits>与C库头具有相同的内容<limits.h>.

在ISO C90标准是棘手弄个,这是考虑到C++依赖于一种耻辱,但部分"数量限制"(随机草案我找到了在一个场合编号2.2.4.2,并已躺在附近)给出INT_MAX等常数的最小值<limits.h>.例如 ULONG_MAX必须至少4294967295,从中推导出的宽度long为至少32位.

C99标准中有类似的限制,但当然不是C++ 03引用的那些.

这并不能保证long至少有4个字节,因为在C和C++中,"byte"基本上定义为"char",并且不保证CHAR_BIT在C或C++ 中为8.CHAR_BIT == 8由POSIX和Windows保证.


pmg*_*pmg 12

不了解C++.在C你有


                                  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

所以char<= short<= int<= long<=long long

CHAR_BIT*sizeof(char)> = 8
CHAR_BIT*sizeof(短)> = 16
CHAR_BIT*大小(int)> = 16
CHAR_BIT*sizeof(长)> = 32
CHAR_BIT*sizeof(long long)> = 64

  • sizeof(char)== 1每个定义.以char为单位的sizeof度量 (4认同)