C++ - <climits>
The C++ <climits> header file 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).
C++ <climits> Macro Constants
The below mentioned macros are implementation-specific and defined with the #define directive. The table below shows the different macros in <climits> header and their minimal or maximal values in all implementations.
Macros | Description | Value |
---|---|---|
CHAR_BIT | Number of bits in a char object (byte) | 8 or greater |
SCHAR_MIN | Minimum value for an object of type signed char | (-27+1) or less |
SCHAR_MAX | Maximum value for an object of type signed char | (27-1) or greater |
UCHAR_MAX | Maximum value for an object of type unsigned char | (28-1) or greater |
CHAR_MIN | Minimum value for an object of type char | either SCHAR_MIN or 0 |
CHAR_MAX | Maximum value for an object of type char | either SCHAR_MAX or UCHAR_MAX |
MB_LEN_MAX | Maximum number of bytes in a multi-byte character, for any locale | 1 or greater |
SHRT_MIN | Minimum value for an object of type short int | (-215+1) or less |
SHRT_MAX | Maximum value for an object of type short int | (215-1) or greater |
USHRT_MAX | Maximum value for an object of type unsigned short int | (216-1) or greater |
INT_MIN | Minimum value for an object of type int | (-215+1) or less |
INT_MAX | Maximum value for an object of type int | (215-1) or greater |
UINT_MAX | Maximum value for an object of type unsigned int | (216-1) or greater |
LONG_MIN | Minimum value for an object of type long int | (-231+1) or less |
LONG_MAX | Maximum value for an object of type long int | (231-1) or greater |
ULONG_MAX | Maximum value for an object of type unsigned long int | (232-1) or greater |
LLONG_MIN (C++11) | Minimum value for an object of type long long int | (-263+1) or less |
LLONG_MAX (C++11) | Maximum value for an object of type long long int | (263-1) or greater |
ULLONG_MAX (C++11) | Maximum value for an object of type unsigned long long int | (264-1) or greater |
Example:
The example below describes the working of macros constant of C++ <climits> library.
#include <iostream> #include <climits> using namespace std; int main (){ cout<<"CHAR_BIT: "<<CHAR_BIT<<"\n"; cout<<"CHAR_MIN: "<<CHAR_MIN<<"\n"; cout<<"CHAR_MAX: "<<CHAR_MAX<<"\n"; cout<<"SHRT_MIN: "<<SHRT_MIN<<"\n"; cout<<"SHRT_MAX: "<<SHRT_MAX<<"\n"; cout<<"LONG_MIN: "<<LONG_MIN<<"\n"; cout<<"LONG_MAX: "<<LONG_MAX<<"\n"; cout<<"LLONG_MIN: "<<LLONG_MIN<<"\n"; cout<<"LLONG_MAX: "<<LLONG_MAX<<"\n"; return 0; }
The output of the above code is machine dependent. One of the possible output could be:
CHAR_BIT: 8 CHAR_MIN: -128 CHAR_MAX: 127 SHRT_MIN: -32768 SHRT_MAX: 32767 LONG_MIN: -9223372036854775808 LONG_MAX: 9223372036854775807 LLONG_MIN: -9223372036854775808 LLONG_MAX: 9223372036854775807