0

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?

Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79
hakuna matata
  • 3,243
  • 13
  • 56
  • 93
  • 1
    On Mac OS X 64-bit apps, `ULONG_MAX` = `ULONGLONG_MAX`, and `ULONG_MAX` ≠ `UINT_MAX`. Specifically `UINT_MAX=4294967295`, `ULONG_MAX=18446744073709551615`, and `ULONG_LONG_MAX=18446744073709551615` – bobobobo Apr 15 '13 at 02:46

3 Answers3

5

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-defined values shall be equal or greater in magnitude (absolute value) to those shown, with the same sign.

cnicutar
  • 178,505
  • 25
  • 365
  • 392
1

On 32-bit platforms, int and long are usually the same, and so their max are also the same. The same for their unsigned counterparts of course.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

The appendix is wrong. The only guarantee is that sizeof(long) >= sizeof(int) >= sizeof(char) note the possibility of equality.

Ron Fox
  • 11
  • 3