C Standard Library -
Introduction
is a header file in the C Standard Library that defines the limits of various data types. These macros provide information about integer types (char, short, int, long and long long etc.) and the maximum and minimum values of other data types.
These limits specify that a variable cannot store any value beyond them; for example, the maximum value an unsigned type can store is 255.
Library macros
The following values are implementation-specific and are defined through #define directives; none of these values may be lower than the values given below.
| macro | Description | Value |
|---|---|---|
| Character type | ||
CHAR_BIT |
char Number of bits in the type |
Typically 8 |
CHAR_MIN |
char Minimum value of the type (signed or unsigned) |
-128 or 0 |
CHAR_MAX |
char Maximum value of the type (signed or unsigned) |
127 or 255 |
SCHAR_MIN |
signed char Minimum value of the type |
-128 |
SCHAR_MAX |
signed char The maximum value of a type |
127 |
UCHAR_MAX |
unsigned char The maximum value of a type |
255 |
| Short integer type | ||
SHRT_MIN |
short Minimum value of the type |
-32768 |
SHRT_MAX |
short The maximum value of a type |
32767 |
USHRT_MAX |
unsigned short The maximum value of a type |
65535 |
| Integer type | ||
INT_MIN |
int Minimum value of the type |
-2147483648 |
INT_MAX |
int The maximum value of a type |
2147483647 |
UINT_MAX |
unsigned int The maximum value of a type |
4294967295 |
| Long integer type | ||
LONG_MIN |
long Minimum value of the type |
-9223372036854775808L |
LONG_MAX |
long The maximum value of a type |
9223372036854775807L |
ULONG_MAX |
unsigned long The maximum value of a type |
18446744073709551615UL |
| Long long integer type | ||
LLONG_MIN |
long long Minimum value of the type |
-9223372036854775808LL |
LLONG_MAX |
long long The maximum value of a type |
9223372036854775807LL |
ULLONG_MAX |
unsigned long long The maximum value of a type |
18446744073709551615ULL |
Example
The following example demonstrates the use of some of the constants defined in the limit.h file.
Example
#include
#include
int
main
(
)
{
printf
(
"Character types:\n"
)
;
printf
(
"CHAR_BIT: %d\n"
,
CHAR_BIT
)
;
printf
(
"CHAR_MIN: %d\n"
,
CHAR_MIN
)
;
printf
(
"CHAR_MAX: %d\n"
,
CHAR_MAX
)
;
printf
(
"SCHAR_MIN: %d\n"
,
SCHAR_MIN
)
;
printf
(
"SCHAR_MAX: %d\n"
,
SCHAR_MAX
)
;
printf
(
"UCHAR_MAX: %u\n"
,
UCHAR_MAX
)
;
printf
(
"\nShort integer types:\n"
)
;
printf
(
"SHRT_MIN: %d\n"
,
SHRT_MIN
)
;
printf
(
"SHRT_MAX: %d\n"
,
SHRT_MAX
)
;
printf
(
"USHRT_MAX: %u\n"
,
USHRT_MAX
)
;
printf
(
"\nInteger types:\n"
)
;
printf
(
"INT_MIN: %d\n"
,
INT_MIN
)
;
printf
(
"INT_MAX: %d\n"
,
INT_MAX
)
;
printf
(
"UINT_MAX: %u\n"
,
UINT_MAX
)
;
printf
(
"\nLong integer types:\n"
)
;
printf
(
"LONG_MIN: %ld\n"
,
LONG_MIN
)
;
printf
(
"LONG_MAX: %ld\n"
,
LONG_MAX
)
;
printf
(
"ULONG_MAX: %lu\n"
,
ULONG_MAX
)
;
printf
(
"\nLong long integer types:\n"
)
;
printf
(
"LLONG_MIN: %lld\n"
,
LLONG_MIN
)
;
printf
(
"LLONG_MAX: %lld\n"
,
LLONG_MAX
)
;
printf
(
"ULLONG_MAX: %llu\n"
,
ULLONG_MAX
)
;
return
0
;
}
Let's compile and run the above program, which will produce the following result:
Character types:
CHAR_BIT: 8
CHAR_MIN: -128
CHAR_MAX: 127
SCHAR_MIN: -128
SCHAR_MAX: 127
UCHAR_MAX: 255
Short integer types:
SHRT_MIN: -32768
SHRT_MAX: 32767
USHRT_MAX: 65535
Integer types:
INT_MIN: -2147483648
INT_MAX: 2147483647
UINT_MAX: 4294967295
Long integer types:
LONG_MIN: -9223372036854775808
LONG_MAX: 9223372036854775807
ULONG_MAX: 18446744073709551615
Long long integer types:
LLONG_MIN: -9223372036854775808
LLONG_MAX: 9223372036854775807
ULLONG_MAX: 18446744073709551615
Provides many macros related to integer types to describe the limits of various data types. These macros are very useful for writing robust and portable code, because they allow programmers to easily obtain the limit values of data types on different platforms.