Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Data Type Ranges and Their Macros in C++



In some cases, especially in competitive programming, we may need to specify the minimum or maximum value of a specific datatype. In C++, each data type has a different memory range under which we can define and declare the value of that data type. But it becomes difficult to remember all the large ranges of each data type.

So, C++ has introduced the macros that are used to represent the minimum and maximum range of some datatype. And some data types do not have macros for minimum values, because they are unsigned (means, hold only positive value). So, as their entire range is shifted to positive values only, therefore their range starts from 0 and extends to double the maximum value of their signed counter parts. For example char ranges -128 to +127 and unsigned char ranges 0 to 255.

These macros are defined in the <climits> header file for integer types (like int, long, etc.), and in the <cfloat> header file for floating-point types (like float and double).

Example

The macro is defined by inserting _MIN and _MAX to the data type name. For example, you want to find the minimum and maximum value of the character type which is represented as char(signed), then it will be represented as CHAR_MIN and CHAR_MAX. So we will get the range of char equals to -128 to +128.

Table of Data Types with Range Macros

Here is the following Table for each data type with their macros in C++.

Data Type Range Macro for min value Macro for max value
int -2147483648 to +2147483647 INT_MIN INT_MAX
long int -2147483648 to +2147483647 LONG_MIN LONG_MAX
long long int -9223372036854775808 to +9223372036854775807 LLONG_MIN LLONG_MAX
unsigned int 0 to 4294967295 ----- INT_MAX
unsigned long int 0 to 18446744073709551615 ----- ULONG_MAX
unsigned long long int 0 to 18446744073709551615 ----- ULLONG_MAX
short int -32768 to +32767 SHRT_MIN SHRT_MAX
unsigned short int 0 to 65535 ----- USHRT_MAX
char -128 to +127 CHAR_MIN CHAR_MAX
short char -128 to +127 SCHAR_MIN SCHAR_MAX
unsigned char 0 to 255 ----- UCHAR_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

Example of Macros for Data Type Ranges

Here is the following simple program to print the range of some data types of C++

#include<iostream>
#include<limits.h> // header file for int,char macros
#include<float.h> // header file for float,double macros
using namespace std;
int main() {
   cout << "char Range: (" << CHAR_MIN <<" to " <<CHAR_MAX << ")\n";
   cout << "short char Range: (" << SCHAR_MIN <<" to " <<SCHAR_MAX << ")\n";
   cout << "unsigned char Range: (" << 0 <<" to " <<UCHAR_MAX << ")\n";
   cout << "short int Range: (" << SHRT_MIN <<" to " <<SHRT_MAX << ")\n";
   cout << "unsigned short int Range: (" << 0 <<" to " <<USHRT_MAX << ")\n";
   cout << "int Range: (" << INT_MIN <<" to " <<INT_MAX << ")\n";
   cout << "unsigned int Range: (" << 0 <<" to " <<UINT_MAX << ")\n";
   cout << "long int Range: (" << LONG_MIN <<" to " <<LONG_MAX << ")\n";
   cout << "unsigned long int Range: (" << 0 <<" to " <<ULONG_MAX << ")\n";
   cout << "long long int Range: (" << LLONG_MIN <<" to " <<LLONG_MAX << ")\n";
   cout << "unsigned long long int Range: (" << 0 <<" to " <<ULLONG_MAX << ")\n";
   cout << "float Range: (" << FLT_MIN <<", " <<FLT_MAX << ")\n";
   cout << "float(negative) Range: (" << -FLT_MIN <<" to " <<-FLT_MAX << ")\n";
   cout << "double Range: (" << DBL_MIN <<" to " <<DBL_MAX << ")\n";
   cout << "double(negative) Range: (" << -DBL_MIN <<" to " <<-DBL_MAX << ")";
}

Output

char Range: (-128 to 127)
short char Range: (-128 to 127)
unsigned char Range: (0 to 255)
short int Range: (-32768 to 32767)
unsigned short int Range: (0 to 65535)
int Range: (-2147483648 to 2147483647)
unsigned int Range: (0 to 4294967295)
long int Range: (-9223372036854775808 to 9223372036854775807)
unsigned long int Range: (0 to 18446744073709551615)
long long int Range: (-9223372036854775808 to 9223372036854775807)
unsigned long long int Range: (0 to 18446744073709551615)
float Range: (1.17549e-38, 3.40282e+38)
float(negative) Range: (-1.17549e-38 to -3.40282e+38)
double Range: (2.22507e-308 to 1.79769e+308)
double(negative) Range: (-2.22507e-308 to -1.79769e+308)
Updated on: 2025-05-29T18:56:08+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements