C Standard Library - <limits.h>
Introduction
limits.h The header file determines various attributes of various variable types. Macros defined in this header file limit the values of various variable types (such as char, int, and long).
These limits specify that the variable cannot store any value that exceeds these limits. For example, the maximum value that an unsigned variable can store is 255.
Library Macros
The following values are implementation specific and are defined through the #define directive. These values must not be lower than the values given below.
Macro | Value | Description |
---|---|---|
CHAR_BIT | 8 | Define the number of bits in a byte. |
SCHAR_MIN | -128 | Defines the minimum value of a signed character. |
SCHAR_MAX | 127 | Defines the maximum value of a signed character. |
UCHAR_MAX | 255 | Defines the maximum value of an unsigned character. |
CHAR_MIN | 0 | Defines the minimum value of type char. If char represents a negative value, its value is equal to SCHAR_MIN, otherwise it is equal to 0. |
CHAR_MAX | 127 | Define the maximum value of type char. If char represents a negative value, its value is equal to SCHAR_MAX, otherwise it is equal to UCHAR_MAX. |
MB_LEN_MAX | 1 | Defines the maximum number of bytes in a multibyte character. |
SHRT_MIN | -32768 | Define the minimum value of a short integer. |
SHRT_MAX | +32767 | Define the maximum value of a short integer. |
USHRT_MAX | 65535 | Defines the maximum value of an unsigned short integer. |
INT_MIN | -32768 | Define the minimum value of an integer. |
INT_MAX | +32767 | Define the maximum value of an integer. |
UINT_MAX | 65535 | Define the maximum value of an unsigned integer. |
LONG_MIN | -2147483648 | Define the minimum value of a long integer. |
LONG_MAX | +2147483647 | Define the maximum value of a long integer. |
ULONG_MAX | 4294967295 | Define the maximum value of an unsigned long integer. |
Example
The following example demonstrates the use of some constants defined in the limit.h file.
#include <stdio.h>#include <limits.h>int main(){ printf("The number of bits in a byte %d\n", CHAR_BIT); printf("The minimum value of SIGNED CHAR = %d\n", SCHAR_MIN); printf("The maximum value of SIGNED CHAR = %d\n", SCHAR_MAX); printf("The maximum value of UNSIGNED CHAR = %d\n", UCHAR_MAX); printf("The minimum value of SHORT INT = %d\n", SHRT_MIN); printf("The maximum value of SHORT INT = %d\n", SHRT_MAX); printf("The minimum value of INT = %d\n", INT_MIN); printf("The maximum value of INT = %d\n", INT_MAX); printf("The minimum value of CHAR = %d\n", CHAR_MIN); printf("The maximum value of CHAR = %d\n", CHAR_MAX); printf("The minimum value of LONG = %ld\n", LONG_MIN); printf("The maximum value of LONG = %ld\n", LONG_MAX); return(0);}
Let us compile and run the above program, which will produce the following results:
The number of bits in a byte 8The minimum value of SIGNED CHAR = -128The maximum value of SIGNED CHAR = 127The maximum value of UNSIGNED CHAR = 255The minimum value of SHORT INT = -32768The maximum value of SHORT INT = 32767The minimum value of INT = -32768The maximum value of INT = 32767The minimum value of CHAR = -128The maximum value of CHAR = 127The minimum value of LONG = -2147483648The maximum value of LONG = 2147483647