Data types ranges and their macros in C++


Given the task is to find the memory range of the different data types, that what range of value a data type can store the value from minimum value to maximum value. There is memory range of data type from in which value of data can be stored. It is difficult to remember the large range of value so C++ has macros for represent these numbers, from macros these large numbers can be directly assigned to variable without typing the whole Number range.

Example

‘char’(signed) is character data type and has a range of -128 to +128 and macro for minimum value, macro to find the range values of char data type is CHAR_MIN and CHAR_MAX.

Likewise we can find range of any data type by these MIN and MAX macros like for ‘int’ we can use INT_MIN and INT_MAX.

Range of char

128 to +128

Range of int

-2147483648 to +2147483648

Example

C++ code to demonstrate the macros of data types

#include<iostream.h>
#include<float.h>
#include<limits.h>
Using namespace std;
int main( ) {
   cout<< " Range of Char :” << CHAR_MIN<< " to ”<<CHAR_MAX;
   cout<< " Range of int :” <<INT_MIN<< " to " <<INT_MAX;
   cout<< " Range of float :” <<FLT_MIN<< " to " <<FLT_MAX;
   cout<< " Range of double :” <<DBL_MIN<< " to " <<DBL_MAX;
   cout<< " Range of short char :” <<SCHAR_MIN<< " to " <<SCHAR_MAX;
   cout<< " Range of Unsigned Char :” << 0 << " to " <<UCHAR_MAX;
   cout<< " Range of long int :” <<LONG_MIN<< " to " <<LONG_MAX;
   cout<< " Range of Unsigned int :” << 0 << " to " <<UINT_MAX;
   cout<< " Range of short int :” <<SHRT_MIN<< " to " <<SHRT_MAX;
   cout<< " Range of float Negative :” <<-FLT_MIN<< " to " <<-FLT_MAX;
   cout<< " Range of double negative:” <<-DBL_MIN<< " to " <<-DBL_MAX;
   Return 0;
}

Output

If we run the above code it will generate the following output −

Range of char: -128 to 127
Range of int: -2147483648 to 2147483648
Range of float: 1.17549e-38 to 3.40282e+38
Range of double: 2.22507e-308 to 1.79769e+308
Range of Short char: -128 to 127
Range of Unsigned Char: 0 to 255
Range of long int: -922337203685477580 to 922337203685477580
Range of Unsigned int: 0 to 42944967295
Range of Short int: -32768 to 32767
Range of float negative: -1.17549e-38 to -3.40282e+38
Range of double negative: 2.22507e-308 to 1.79769e+308

Updated on: 23-Mar-2020

129 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements