C Standard Library Limits H
## Introduction
`` is a header file in the C standard library that defines limits for various data types. These macros provide information about the maximum and minimum values for integer types (such as `char`, `short`, `int`, `long`, and `long long`) and other data types.
These limits specify that variables cannot store any value exceeding these boundsβfor example, the maximum value an unsigned type can store is 255.
## Library Macros
The following values are implementation-specific and are defined using `#define` directives. The actual values must not be lower than those listed below.
| **Macro** | **Description** | **Value** |
| --- | --- | --- |
| **Character Types** | | |
| `CHAR_BIT` | Number of bits in a `char` | Typically 8 |
| `CHAR_MIN` | Minimum value of `char` (signed or unsigned) | -128 or 0 |
| `CHAR_MAX` | Maximum value of `char` (signed or unsigned) | 127 or 255 |
| `SCHAR_MIN` | Minimum value of `signed char` | -128 |
| `SCHAR_MAX` | Maximum value of `signed char` | 127 |
| `UCHAR_MAX` | Maximum value of `unsigned char` | 255 |
| **Short Integer Types** | | |
| `SHRT_MIN` | Minimum value of `short` | -32768 |
| `SHRT_MAX` | Maximum value of `short` | 32767 |
| `USHRT_MAX` | Maximum value of `unsigned short` | 65535 |
| **Integer Types** | | |
| `INT_MIN` | Minimum value of `int` | -2147483648 |
| `INT_MAX` | Maximum value of `int` | 2147483647 |
| `UINT_MAX` | Maximum value of `unsigned int` | 4294967295 |
| **Long Integer Types** | | |
| `LONG_MIN` | Minimum value of `long` | -9223372036854775808L |
| `LONG_MAX` | Maximum value of `long` | 9223372036854775807L |
| `ULONG_MAX` | Maximum value of `unsigned long` | 18446744073709551615UL |
| **Long Long Integer Types** | | |
| `LLONG_MIN` | Minimum value of `long long` | -9223372036854775808LL |
| `LLONG_MAX` | Maximum value of `long long` | 9223372036854775807LL |
| `ULLONG_MAX` | Maximum value of `unsigned long long` | 18446744073709551615ULL |
## Example
The following example demonstrates the usage of some constants defined in the `limits.h` header 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("\n Short 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("\n Integer types:\n");
printf("INT_MIN: %d\n", INT_MIN);
printf("INT_MAX: %d\n", INT_MAX);
printf("UINT_MAX: %u\n", UINT_MAX);
printf("\n Long 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("\n Long 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 program above, which will produce the following output:
Character types: CHAR_BIT: 8 CHAR_MIN: -128 CHAR_MAX: 127 SCHAR_MIN: -128 SCHAR_MAX: 127 UCHAR_MAX: 255Short integer types: SHRT_MIN: -32768 SHRT_MAX: 32767 USHRT_MAX: 65535Integer types: INT_MIN: -2147483648 INT_MAX: 2147483647 UINT_MAX: 4294967295Long integer types: LONG_MIN: -9223372036854775808 LONG_MAX: 9223372036854775807 ULONG_MAX: 18446744073709551615Long long integer types: LLONG_MIN: -9223372036854775808 LLONG_MAX: 9223372036854775807 ULLONG_MAX: 18446744073709551615
`` provides numerous macros related to integer types, describing the limits of various data types. These macros are extremely useful for writing robust and portable code, as they allow programmers to easily retrieve the limit values of data types across different platforms.
YouTip