gpt4 book ai didi

c++ - 关于C++中unsigned char类型的大小

转载 作者:太空狗 更新时间:2023-10-29 19:52:00 25 4
gpt4 key购买 nike

标准 C++ [3.9.1-1] 是这样说的

For unsigned character types, all possible bit patterns of the value representation represent numbers.

标准[18.3.2.4-(comment 197)]也说unsigned char的最大值相当于C中的UCHAR_MAX,在C标准[5.2.4.2]中定义为255 .1]。那么这是否意味着 C++ 中 unsigned char 类型的大小恰好是 8 位?

最佳答案

来自 C11 5.2.4.2.1

The values given below shall be replaced by constant expressions suitable for use in #if preprocessing directives. Moreover, except for CHAR_BIT and MB_LEN_MAX, the following shall be replaced by expressions that have the same type as would an expression that is an object of the corresponding type converted according to the integer promotions. Their implementation-defined values shall be equal or greater in magnitude (absolute value) to those shown, with the same sign.

(强调我的)

因此标准定义 UCHAR_MAX 至少需要为 255,但可以大于该值。

我们对尺寸的保证是:

sizeof(char) = 1 and sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long)

并且数据类型的签名版本至少必须能够容纳:

  • char [-127, 127]
  • 短 [-32767, 32767]
  • int [-32767, 32767]
  • 长 [-2147483647, 2147483647]
  • long long [-9223372036854775807, 9223372036854775807]

关于c++ - 关于C++中unsigned char类型的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32785675/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com