![]() |
VOOZH | about |
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.
Example: The following program displays the ranges of several built-in data types using predefined macros
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:...
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.
Modern C++ provides the numeric_limits class template as a type-safe alternative to range macros. It is defined in the <limits> header.
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().
Compared to macros, numeric_limits offers several benefits: