The maximum and minimum size of integral values are quite useful or in simple terms, limits of any integral type plays quite a role in programming. Instead of remembering these values, different macros can be used.
<climits>(limits.h) defines sizes of integral types.
- This header defines constants with the limits of fundamental integral types for the specific system and compiler implementation used.
- The limits for fundamental floating-point types are defined in <cfloat> (<float.h>).
- The limits for width-specific integral types and other typedef types are defined in <cstdint> (<stdint.h>).
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*
NOTE: The actual value depends on the particular system and library implementation but shall reflect the limits of these types in the target platform. Your Machine’s values might depend upon whether it is a 32-bit machine or 64-bit machine.
Compatibility :
LLONG_MIN, LLONG_MAX, and ULLONG_MAX are defined for libraries complying with the C standard of 1999 or later (which only includes the C++ standard since 2011: C++11).
Two Applications of these MACROS are Checking for integer overflow and Computing minimum or maximum in an array of very large or very small elements.
Below Program will display the respective values for your machine:
// C++ program to demonstrate working of // constants in climits. #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