limits.h in C Language
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 signedchar
.SCHAR_MAX
: Maximum value of a signedchar
.UCHAR_MAX
: Maximum value of an unsignedchar
.CHAR_MIN
: Minimum value of achar
.CHAR_MAX
: Maximum value of achar
.MB_LEN_MAX
: Maximum number of bytes in a multibyte character.SHRT_MIN
: Minimum value of ashort int
.SHRT_MAX
: Maximum value of ashort int
.USHRT_MAX
: Maximum value of an unsignedshort int
.INT_MIN
: Minimum value of anint
.INT_MAX
: Maximum value of anint
.UINT_MAX
: Maximum value of an unsignedint
.LONG_MIN
: Minimum value of along int
.LONG_MAX
: Maximum value of along int
.ULONG_MAX
: Maximum value of an unsignedlong int
.LLONG_MIN
: Minimum value of along long int
.LLONG_MAX
: Maximum value of along long int
.ULLONG_MAX
: Maximum value of an unsignedlong long int
.
Here’s an example of using preprocessor directives to check if an int
type can store values larger than 100,000:
1 |
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 |
|
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
.
All articles on this blog are licensed under CC BY-NC-SA 4.0 unless otherwise stated.