limits.h
limits.h
provides macros for defining the range of values taken by various integer types (including character types).
CHAR_BIT
: the number of binary bits each character contains.SCHAR_MIN
: the minimum value of the signed char type.SCHAR_MAX
: the maximum value of the signed char type.UCHAR_MAX
: the maximum value of the unsiged char type.CHAR_MIN
: the minimum value of the char type.CHAR_MAX
: maximum value of the char type.MB_LEN_MAX
: the maximum number of bytes a multi-byte character can contain.SHRT_MIN
: the minimum value of the short int type.SHRT_MAX
: maximum value of the short int type.USHRT_MAX
: maximum value of type unsigned short int.INT_MIN
: minimum value of type int.INT_MAX
: maximum value of type int.UINT_MAX
: maximum value of type unsigned int.LONG_MIN
: the smallest value of type long int.LONG_MAX
: maximum value of type long int.ULONG_MAX
: maximum value of type unsigned long int.LLONG_MIN
: the smallest value of type long long int.LLONG_MAX
: maximum value of type long long int.ULLONG_MAX
: maximum value of type unsigned long long int.
The following example uses the preprocessor instruction to determine whether the int type can be used to store numbers greater than 100000.
#if INT_MAX < 100000
#error int type is too small
#endif
In the above example, the preprocessor will display an error message if the int type is too small.
You can use the macro inside limit.h
to select the correct underlying type for the type alias.
#if INT_MAX >= 100000
typedef int Quantity;
#else
typedef long int Quantity;
#endif
In the above example, the type alias Quantity
points to int
if the maximum value of the integer type (INT_MAX
) is not less than 100000, otherwise it points to long int
.