Every fundamental data type in C++ can store values only within a specific range. The standard library provides predefined macros that represent the minimum and maximum values of these types, making it easy to obtain their limits in a portable manner.
- Integer type limits are defined in the <climits> header.
- Floating-point type limits are defined in the <cfloat> header.
- These macros help avoid hardcoding implementation-specific values.
Example: The following program displays the ranges of several built-in data types using predefined macros
#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 << "\nshort 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 short int ranges from: -32768 to 32767 int ranges from: -2147483648 to 2147483647 long int ranges from: -9223372036854775808 to 9223372036854775807 float ranges from:...
Common Data Type Range Macros
A list of some of the data type macros is mentioned below considering GCC compiler as of Dec 2024.
Data Type | Range | Macro for min value | Macro for max value |
|---|---|---|---|
char | -128 to +127 | CHAR_MIN | CHAR_MAX |
signed 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) | -3.40282e+38 to -1.17549e-38 | -FLT_MIN | -FLT_MAX |
double | 2.22507e-308 to 1.79769e+308 | DBL_MIN | DBL_MAX |
double (negative) | -3.40282e+38 to -1.17549e-38 | -DBL_MIN | -DBL_MAX |
Note: Exact ranges depend on the compiler, platform, and architecture.
Data Type Limits Using numeric_limits
Modern C++ provides the numeric_limits class template as a type-safe alternative to range macros. It is defined in the <limits> header.
#include <iostream>
#include <limits>
using namespace std;
int main() {
cout << "short int ranges from: "
<< numeric_limits<short>::min()
<< " to "
<< numeric_limits<short>::max()
<< endl;
cout << "int ranges from: "
<< numeric_limits<int>::min()
<< " to "
<< numeric_limits<int>::max()
<< endl;
cout << "float 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 float ranges from: 1.17549e-38 to 3.40282e+38
Explanation: numeric_limits<T> provides information about the properties and limits of a data type T through member functions such as min() and max().
Advantages of numeric_limits
Compared to macros, numeric_limits offers several benefits:
- Provides type-safe access to data type limits.
- Works naturally with templates and generic programming.
- Improves readability and maintainability.
- Offers additional information beyond minimum and maximum values.