gpt4 book ai didi

c - 为什么 C 标准将数据类型的范围定义为一个短?

转载 作者:太空狗 更新时间:2023-10-29 14:51:59 26 4
gpt4 key购买 nike

C99 标准以下列方式定义数据类型的范围:

— minimum value for an object of type signed char
SCHAR_MIN -127 // −(2^7 − 1)
— maximum value for an object of type signed char
SCHAR_MAX +127 // 2^7 − 1
— maximum value for an object of type unsigned char
UCHAR_MAX 255 // 2^8 − 1
— minimum value for an object of type char
CHAR_MIN see below
— maximum value for an object of type char
CHAR_MAX see below
— maximum number of bytes in a multibyte character, for any supported locale
MB_LEN_MAX 1
— minimum value for an object of type short int
SHRT_MIN -32767 // −(2^15 − 1)
— maximum value for an object of type short int
SHRT_MAX +32767 // 2^15 − 1
— maximum value for an object of type unsigned short int
USHRT_MAX 65535 // 2^16 − 1
— minimum value for an object of type int
INT_MIN -32767 // −(2^15 − 1)
— maximum value for an object of type int
INT_MAX +32767 // 2^15 − 1
— maximum value for an object of type unsigned int
UINT_MAX 65535 // 2^16 − 1
— minimum value for an object of type long int
LONG_MIN -2147483647 // −(2^31 − 1)
— maximum value for an object of type long int
LONG_MAX +2147483647 // 2^31 − 1
— maximum value for an object of type unsigned long int
ULONG_MAX 4294967295 // 2^32 − 1

如果我们看到负范围,它实际上可能比此处根据允许的二进制补码表示定义的范围多一。为什么这样定义它们?

最佳答案

If we see the negative range, it can be actually one more than what is defined here as per allowable two's complement representations. Why they are defined like this ?

因为 C 也是为旧的(和新的!)架构设计的,这些架构不一定对有符号整数使用二进制补码表示。 C11 标准确实允许三种表示形式(其中哪些适用是实现定义的):

§ 6.2.6.2 Integer types

If the sign bit is one, the value shall be modified in one of the following ways:

— the corresponding value with sign bit 0 is negated (sign and magnitude)
— the sign bit has the value −(2M ) (two’s complement);
— the sign bit has the value −(2M − 1) (ones’ complement).

因此,对于 ones 的补码表示,最小值是 -(2^M - 1)。然而,有一个异常(exception):C99 可选类型 intxx_t,保证以二进制补码表示形式存储(这就是为什么有可选类型:C 标准不强制这种表示形式)。

关于c - 为什么 C 标准将数据类型的范围定义为一个短?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12957574/

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