首页  >  文章  >  后端开发  >  C 中的“long”保证至少为 32 位吗?

C 中的“long”保证至少为 32 位吗?

Susan Sarandon
Susan Sarandon原创
2024-10-26 18:10:30670浏览

Is `long` Guaranteed to be at Least 32 Bits in C  ?

long 保证至少为 32 位吗?

C 标准规定了整数数据存储方面的以下优先顺序基本类型:

sizeof(char) ≤ sizeof(short int) ≤ sizeof(int) ≤ sizeof(long int)

第 1.7/1 节中提供了有关 char 大小的进一步说明:

The fundamental storage unit in the C++ memory model is the byte. A byte is at least large enough to contain any member of the implementation’s basic character set and is composed of a contiguous sequence of bits, the number of which is implementation-defined.

这表明 sizeof(char) 可以是 1 到 n 之间的任何值位,其中 n 取决于实现。然而,C 标准还需要了解 C 标准 (1.2/1),它定义了 long 可以容纳的值的最小限制:

LONG_MIN = -2147483647 // -(2^31 - 1)
LONG_MAX = +2147483647 //   2^31 - 1

因为 long 必须能够同时容纳正数和负数如果值达到这些限制,则从逻辑上推断出 long 的大小必须至少为 32 位。

简而言之,虽然 C 标准未指定字节中的位数,但对 LONG_MIN 施加的最小限制LONG_MAX 意味着 long 必须至少有 32 位存储空间。

以上是C 中的“long”保证至少为 32 位吗?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn