The <climits> (or <limits.h>) header file provides predefined constants that define the minimum and maximum limits of integral data types in C++. These constants help write portable programs without hardcoding platform-specific values.
- Defines the minimum and maximum values for fundamental integral data types such as int, char, long, and short.
- Improves portability by providing system-specific limits through predefined macros instead of hardcoded values.
Different macro constants are
1. CHAR_MIN:
Minimum value for an object of type char
Value of CHAR_MIN is either -127 (-27+1) or less* or 0
2. CHAR_MAX:
Maximum value for an object of type char
Value of CHAR_MAX is either 127 (27-1) or 255 (28-1) or greater*
3. SHRT_MIN:
Minimum value for an object of type short int
Value of SHRT_MIN is -32767 (-215+1) or less*
4. SHRT_MAX:
Maximum value for an object of type short int
Value of SHRT_MAX is 32767 (215-1) or greater*
5. USHRT_MAX:
Maximum value for an object of type unsigned short int
Value of USHRT_MAX is 65535 (216-1) or greater*
6. INT_MIN:
Minimum value for an object of type int
Value of INT_MIN is -2147483648 (-231) or less*
7. INT_MAX:
Maximum value for an object of type int
Value of INT_MAX is 2147483647 ( 231-1)
8. UINT_MAX:
Maximum value for an object of type unsigned int
Value of UINT_MAX is 4294967295 (232-1) or greater*
9. LONG_MIN:
Minimum value for an object of type long int
Value of LONG_MIN is -2147483647 (-231+1) or less*
10. LONG_MAX:
Maximum value for an object of type long int
Value of LONG_MAX is 2147483647 (231-1) or greater*
11. ULONG_MAX:
Maximum value for an object of type unsigned long int
Value of ULONG_MAX is 4294967295 (232-1) or greater*
12. LLONG_MIN:
Minimum value for an object of type long long int
Value of LLONG_MIN is -9223372036854775807 (-263+1) or less*
13. LLONG_MAX:
Maximum value for an object of type long long int
Value of LLONG_MAX is 9223372036854775807 (263-1) or greater*
14. ULLONG_MAX:
Maximum value for an object of type unsigned long long int
Value of ULLONG_MAX is 18446744073709551615 (264-1) or greater*
#include <climits>
#include <iostream>
using namespace std;
int main()
{
cout << "CHAR_MIN : " << CHAR_MIN << endl;
cout << "CHAR_MAX : " << CHAR_MAX << endl;
cout << "SHRT_MIN : " << SHRT_MIN << endl;
cout << "SHRT_MAX : " << SHRT_MAX << endl;
cout << "USHRT_MAX : " << USHRT_MAX << endl;
cout << "INT_MIN : " << INT_MIN << endl;
cout << "INT_MAX : " << INT_MAX << endl;
cout << "UINT_MAX : " << UINT_MAX << endl;
cout << "LONG_MIN : " << LONG_MIN << endl;
cout << "LONG_MAX : " << LONG_MAX << endl;
cout << "ULONG_MAX : " << ULONG_MAX << endl;
cout << "LLONG_MIN : " << LLONG_MIN << endl;
cout << "LLONG_MAX : " << LLONG_MAX << endl;
cout << "ULLONG_MAX : " << ULLONG_MAX << endl;
return 0;
}
Output (Machine dependent)
CHAR_MIN : -128
CHAR_MAX : 127
SHRT_MIN : -32768
SHRT_MAX : 32767
USHRT_MAX : 65535
INT_MIN : -2147483648
INT_MAX : 2147483647
UINT_MAX : 4294967295
LONG_MIN : -9223372036854775808
LONG_MAX : 9223372036854775807
ULONG_MAX : 18446744073709551615
LLONG_MIN : -9223372036854775808
LLONG_MAX : 9223372036854775807
ULLONG_MAX : 18446744073709551615