Data Type Ranges and Their Macros in C++
Most of the times, in competitive programming, there is a need to assign the variable, the maximum or minimum value that data type can hold but remembering such a large and precise number comes out to be a difficult job. Therefore, C++ has certain macros to represent these numbers, so that these can be directly assigned to the variable without actually typing the whole number.
The <climits> header file in C++ defines macros that represent the upper and lower bounds of integer data types and <cfloat> defines the macros for float and double limits. These macros allow you to easily assign these extreme values to variables without manually typing them out.
Let's take a look at an example:
#include <iostream>
// for int, char macros
#include <climits>
// for float, double macros
#include <cfloat>
using namespace std;
int main() {
// Displaying ranges with the help of macros
cout << "char ranges from: " << CHAR_MIN << " to "
<< CHAR_MAX << endl;
cout << "\nnshort int ranges from: " << SHRT_MIN
<< " to " << SHRT_MAX << endl;
cout << "\nint ranges from: " << INT_MIN << " to "
<< INT_MAX << endl;
cout << "\nlong int ranges from: " << LONG_MIN << " to "
<< LONG_MAX << endl;
cout << "\nfloat ranges from: " << FLT_MIN << " to "
<< FLT_MAX << endl;
return 0;
}
Output
char ranges from: -128 to 127
nshort int ranges from: -32768 to 32767
int ranges from: -2147483648 to 2147483647
long int ranges from: -9223372036854775808 to 9223372036854775807
float ranges from: 1.17549e-38 to 3.40282e+38
Data Types and Their Range Macros
A list of some of the data type macros is mentioned below:
Data Type | Range | Macro for min value | Macro for max value |
---|---|---|---|
char | -128 to +127 | CHAR_MIN | CHAR_MAX |
short char | -128 to +127 | SCHAR_MIN | SCHAR_MAX |
unsigned char | 0 to 255 | -- | UCHAR_MAX |
short int | -32768 to +32767 | SHRT_MIN | SHRT_MAX |
unsigned short int | 0 to 65535 | -- | USHRT_MAX |
int | -2147483648 to +2147483647 | INT_MIN | INT_MAX |
unsigned int | 0 to 4294967295 | -- | UINT_MAX |
long int | -9223372036854775808 to +9223372036854775807 | LONG_MIN | LONG_MAX |
unsigned long int | 0 to 18446744073709551615 | -- | ULONG_MAX |
long long int | -9223372036854775808 to +9223372036854775807 | LLONG_MIN | LLONG_MAX |
unsigned long long int | 0 to 18446744073709551615 | -- | ULLONG_MAX |
float | 1.17549e-38 to 3.40282e+38 | FLT_MIN | FLT_MAX |
float (negative) | -1.17549e-38 to -3.40282e+38 | -FLT_MIN | -FLT_MAX |
double | 2.22507e-308 to 1.79769e+308 | DBL_MIN | DBL_MAX |
double (negative) | -2.22507e-308 to -1.79769e+308 | -DBL_MIN | -DBL_MAX |
Data Type Limits in Modern C++
The above macro approach for the upper and lower limits of the data type is the old C language approach inherited by C++. But C++ also have its own method to provide programmers with the same information.
C++ offers the numeric_limits<> class template as a modern alternative to these macros. This template provides a more object-oriented approach for accessing data type limits. It is defined inside the <limits> header file.
Let's take a look at an example:
#include <iostream>
#include <limits>
using namespace std;
int main() {
// Displaying ranges with the help of macros
cout << "short int ranges from: " << numeric_limits<short int>::min()
<< " to " << numeric_limits<short int>::max() << endl;
cout << "\nint ranges from: " << numeric_limits<int>::min() << " to "
<< numeric_limits<int>::max() << endl;
cout << "\nlong int ranges from: " << numeric_limits<long>::min() << " to "
<< numeric_limits<long>::max() << endl;
cout << "\nfloat ranges from: " << numeric_limits<float>::min() << " to "
<< numeric_limits<float>::max() << endl;
return 0;
}
Output
short int ranges from: -32768 to 32767
int ranges from: -2147483648 to 2147483647
long int ranges from: -9223372036854775808 to 9223372036854775807
float ranges from: 1.17549e-38 to 3.40282e+38
It is recommended to use this approach to find the upper and lower limits of the data type instead of macros as it is more type safe, and readable compared to the macro-based approach.