Data Type Ranges and their macros in C++

C++Server Side ProgrammingProgramming

In some cases, we need to use the minimum or maximum value of a specific datatype in different problems. It is very difficult to remember that value. For that reason, C++ has some macros, that are used to denote the minimum and maximum range of some datatype. Some of them do not have macros, because they are unsigned, so the minimum will be 0.

Data TypeRangeMacro for min valueMacro for max value
char-128 to +127CHAR_MINCHAR_MAX
short char-128 to +127SCHAR_MINSCHAR_MAX
unsigned char0 to 255-----UCHAR_MAX
short int-32768 to +32767SHRT_MINSHRT_MAX
unsigned short int0 to 65535-----USHRT_MAX
int-2147483648 to +2147483647INT_MININT_MAX
unsigned int0 to 4294967295-----INT_MAX
long int-2147483648 to +2147483647LONG_MINLONG_MAX
unsigned long int0 to 18446744073709551615-----ULONG_MAX
long long int-9223372036854775808 to +9223372036854775807LLONG_MINLLONG_MAX
unsigned long long int0 to 18446744073709551615-----ULLONG_MAX
float1.17549e-38 to 3.40282e+38FLT_MINFLT_MAX
float(negative)-1.17549e-38 to -3.40282e+38-FLT_MIN-FLT_MAX
double2.22507e-308 to 1.79769e+308DBL_MINDBL_MAX
double(negative)-2.22507e-308 to -1.79769e+308-DBL_MIN-DBL_MAX

We can see a simple program to print the range of some data types of C++.

Example

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

Output

char Range: (-128, 127)
short char Range: (-128, 127)
unsigned char Range: (0, 255)
short int Range: (-32768, 32767)
unsigned short int Range: (0, 65535)
int Range: (-2147483648, 2147483647)
unsigned int Range: (0, 4294967295)
long int Range: (-2147483648, 2147483647)
unsigned long int Range: (0, 4294967295)
long long int Range: (-9223372036854775808, 9223372036854775807)
unsigned long long int Range: (0, 18446744073709551615)
float Range: (1.17549e-038, 3.40282e+038)
float(negative) Range: (-1.17549e-038, -3.40282e+038)
double Range: (2.22507e-308, 1.79769e+308)
double(negative) Range: (-2.22507e-308, -1.79769e+308)
raja
Published on 29-Apr-2019 13:15:12
Advertisements