C Standard Library -<limits.h>
Introduction
<limits.h>is a header file in the C standard library, defining limits for various data types. These macros provide information about integer types (char、short、int、longandlong longetc.) and the maximum and minimum values of other data types.
These limits specify that variables cannot store any values beyond these limits, for example, an unsigned can store a maximum value of 255.
Library macros
The following values are implementation-defined and are defined via the #define directive; these values must not be lower than the values given below.
| Macro | Description | Value |
|---|---|---|
| Character types | ||
CHAR_BIT |
charNumber of bits of the type |
Usually 8 |
CHAR_MIN |
charMinimum value of a type (signed or unsigned) |
-128 or 0 |
CHAR_MAX |
charMaximum value of a type (signed or unsigned) |
127 or 255 |
SCHAR_MIN |
signed charMinimum value of the type |
-128 |
SCHAR_MAX |
signed charMaximum value of the type |
127 |
UCHAR_MAX |
unsigned charMaximum value of the type |
255 |
| Short integer type | ||
SHRT_MIN |
shortMinimum value of the type |
-32768 |
SHRT_MAX |
shortMaximum value of the type |
32767 |
USHRT_MAX |
unsigned shortMaximum value of the type |
65535 |
| Integer Types | ||
INT_MIN |
intMinimum value of the type |
-2147483648 |
INT_MAX |
intMaximum value of the type |
2147483647 |
UINT_MAX |
unsigned intMaximum value of the type |
4294967295 |
| Long integer type | ||
LONG_MIN |
longMinimum value of the type |
-9223372036854775808L |
LONG_MAX |
longMaximum value of the type |
9223372036854775807L |
ULONG_MAX |
unsigned longMaximum value of the type |
18446744073709551615UL |
| Long long integer type | ||
LLONG_MIN |
long longMinimum value of the type |
-9223372036854775808LL |
LLONG_MAX |
long longMaximum value of the type |
9223372036854775807LL |
ULLONG_MAX |
unsigned long longMaximum value of the type |
18446744073709551615ULL |
Example
The following example demonstrates the use of some constants defined in the limit.h file.
Example
#include <limits.h>
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 us compile and run the above program, which will produce the following results:
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
<limits.h>It provides many macros related to integer types, used 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.