The limits.h header provides macros that define the range of values for various integer types, including character types. Here are the key macros:

  • CHAR_BIT: Number of bits in a character.
  • SCHAR_MIN: Minimum value of a signed char.
  • SCHAR_MAX: Maximum value of a signed char.
  • UCHAR_MAX: Maximum value of an unsigned char.
  • CHAR_MIN: Minimum value of a char.
  • CHAR_MAX: Maximum value of a char.
  • MB_LEN_MAX: Maximum number of bytes in a multibyte character.
  • SHRT_MIN: Minimum value of a short int.
  • SHRT_MAX: Maximum value of a short int.
  • USHRT_MAX: Maximum value of an unsigned short int.
  • INT_MIN: Minimum value of an int.
  • INT_MAX: Maximum value of an int.
  • UINT_MAX: Maximum value of an unsigned int.
  • LONG_MIN: Minimum value of a long int.
  • LONG_MAX: Maximum value of a long int.
  • ULONG_MAX: Maximum value of an unsigned long int.
  • LLONG_MIN: Minimum value of a long long int.
  • LLONG_MAX: Maximum value of a long long int.
  • ULLONG_MAX: Maximum value of an unsigned long long int.

Here’s an example of using preprocessor directives to check if an int type can store values larger than 100,000:

1
2
3
#if INT_MAX < 100000
#error int type is too small
#endif

In this example, if the int type is too small, the preprocessor will display an error message.

You can use macros from limits.h to select the correct underlying type for a type alias. For example:

1
2
3
4
5
#if INT_MAX >= 100000
typedef int Quantity;
#else
typedef long int Quantity;
#endif

In this example, if the maximum value for the int type (INT_MAX) is at least 100,000, the alias Quantity will be defined as int. Otherwise, it will be defined as long int.