khchehab khchehab - 3 months ago 8
C Question

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.

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
.

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
.

Why is that?

Answer

First of all, the right way to print an unsigned long is not %u but %lu. Second, the standard says the minimum value of UINT_MAX is 65535. It can be (and usually is) larger.

5.2.4.2.1 Sizes of integer types

Their implementation-deļ¬ned values shall be equal or greater in magnitude (absolute value) to those shown, with the same sign.

Comments