[英]UINT_MAX the same as ULONG_MAX in C
While solving exercises from the K&R C book, I stumbled upon the exercise 2.1. 在解决K&R C书中的练习时,我偶然发现了练习2.1。
At first I got as UINT_MAX
as -1
, but then I used the %u
placeholder, but now its giving me the same number as ULONG_MAX
. 起初,我将UINT_MAX
为-1
,但是后来我使用了%u
占位符,但是现在它给了我与ULONG_MAX
相同的数字。
In the book in Appendix B, they say that UINT_MAX
should be 65535
and ULONG_MAX
should be 4294967295
, but when running the exercise, its giving me for both UINT_MAX
and ULONG_MAX
as 4294967295
. 在附录B的书中,他们说UINT_MAX
应该为65535
, ULONG_MAX
应该为4294967295
,但是运行该练习时,它给我的UINT_MAX
和ULONG_MAX
都UINT_MAX
为4294967295
。
Why is that? 这是为什么?
First of all, the right way to print an unsigned long
is not %u
but %lu
. 首先,打印unsigned long
的正确方法不是%u
而是%lu
。 Second, the standard says the minimum value of UINT_MAX
is 65535. It can be (and usually is) larger. 其次,标准说UINT_MAX
的最小值是65535。它可以(通常是)更大。
5.2.4.2.1 Sizes of integer types 5.2.4.2.1整数类型的大小
Their implementation-defined values shall be equal or greater in magnitude (absolute value) to those shown, with the same sign. 其实现定义的值的大小 (绝对值)应等于或大于所示的相同符号。
On 32-bit platforms, int
and long
are usually the same, and so their max are also the same. 在32位平台上, int
和long
通常是相同的,因此它们的最大值也相同。 The same for their unsigned counterparts of course. 当然,对于未签名的同行也是如此。
The appendix is wrong. 附录是错误的。 The only guarantee is that sizeof(long) >= sizeof(int) >= sizeof(char) note the possibility of equality. 唯一的保证是sizeof(long)> = sizeof(int)> = sizeof(char)注意相等的可能性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.