Using suffixes and explicit types to prevent unexpected behavior

The C language limit (in limits.h) is different under LP64 than it is under ILP32. As the following example shows, you can prevent unexpected behavior by an application by using suffixes and explicit types with all numbers.

#ifdef _LP64
#define LONG_MAX (9223372036854775807L)
#define LONG_MIN (-LONG_MAX - 1)
#define ULONG_MAX (18446744073709551615U)
#else
#define LONG_MAX INT_MAX
#define LONG_MIN INT_MIN
#define ULONG_MAX (UINT_MAX)
#endif /* _LP64 */
Note: The output for LONG_MAX is not really -1. The reason for the -1 is that:
  • The printf subroutine handles it as an integer
  • (LONG_MAX == (int)LONG_MAX) returns a negative value