ThunderPhoenix
ThunderPhoenix

Reputation: 1883

definition of UINT_MAX macro

I would like to know if there is a particular reason to define the macro UINT_MAX as (2147483647 * 2U + 1U) and not directly its true value (4294967295U) in the climits header file.

Thank you all.

Upvotes: 1

Views: 3800

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726569

As far as the compiled code is concerned, there would be no difference, because the compiler would evaluate both constant expressions to produce the same value at compile time.

Defining UINT_MAX in terms of INT_MAX lets you reuse a constant that you have already defined:

#define UINT_MAX (INT_MAX * 2U + 1U)

In fact, this is very much what clang's header does, reusing an internal constant __INT_MAX__ for both INT_MAX and UINT_MAX:

#define INT_MAX   __INT_MAX__
#define UINT_MAX  (__INT_MAX__  *2U +1U)

Upvotes: 4

Related Questions