C++ Program to Find Range of Data Types

In this tutorial, we will write the C++ Program to find the range of Data Types using Macro constants or without using any macro constants.

<limits.h> header file is defined to find the range of fundamental data-types. Unsigned modifiers have minimum value is zero. So, no macro constants are defined for the unsigned minimum value.

Macro Constants

NameExpresses
CHAR_MINMinimum value for an object of type char
CHAR_MAXMaximum value for an object of type char
SCHAR_MINMinimum value for an object of type Signed char
SCHAR_MAXMaximum value for an object of type Signed char
UCHAR_MAXMaximum value for an object of type Unsigned char
CHAR_BITNumber of bits in a char object
MB_LEN_MAXMaximum number of bytes in a multi-byte character
SHRT_MINMinimum value for an object of type short int
SHRT_MAXMaximum value for an object of type short int
USHRT_MAXMaximum value for an object of type Unsigned short int
INT_MINMinimum value for an object of type int
INT_MAXMaximum value for an object of type int
UINT_MAXMaximum value for an object of type Unsigned int
LONG_MINMinimum value for an object of type long int
LONG_MAXMaximum value for an object of type long int
ULONG_MAXMaximum value for an object of type Unsigned long int
LLONG_MINMinimum value for an object of type long long int
LLONG_MAXMaximum value for an object of type long long int
ULLONG_MAXMaximum value for an object of type Unsigned long long int

The actual value depends on the particular system and library implementation but shall reflect the limits of these types in the target platform. LLONG_MIN, LLONG_MAX, and ULLONG_MAX are defined for libraries complying with the C standard of 1999 or later (which only includes the C++ standard since 2011: C++11).

C++ Program to Find the Range of Data Types using Macro Constants

#include <iostream>
#include <limits.h>
using namespace std;
int main()
{
  cout << "char minimum value: " << CHAR_MIN << endl;
  cout << "char maximum value: " << CHAR_MAX << endl;
  cout << "Unsigned char maximum value: " << UCHAR_MAX << endl;
  cout << "Signed char minimum value: " << SCHAR_MIN << endl;
  cout << "Signed char maximum value: " << SCHAR_MAX << 
                                           endl << endl;

  cout << "short int minimum value: " << SHRT_MIN << endl;
  cout << "short int maximum value: " << SHRT_MAX << endl;
  cout << "Unsigned short int maximum value: " << USHRT_MAX <<
                                           endl << endl;

  cout << "int minimum value: " << INT_MIN << endl;
  cout << "int maximum value: " << INT_MAX << endl;
  cout << "Unsigned int maximum value: " << UINT_MAX << 
                                           endl << endl;

  cout << "long int minimum value: " << LONG_MIN << endl;
  cout << "long int maximum value: " << LONG_MAX << endl;
  cout << "Unsigned long int maximum value: " << ULONG_MAX 
                                              << endl;

  cout << "long long int minimum value: " << LLONG_MIN << endl;
  cout << "long long int maximum value: " << LLONG_MAX << endl;
  cout << "Unsigned long long int maximum value: " 
                                    << ULLONG_MAX << endl;

  return 0;
}

Output:-

char minimum value: -128
char maximum value: 127
Unsigned char maximum value: 255
Signed char minimum value: -128
Signed char maximum value: 127

short int minimum value: -32768
short int maximum value: 32767
Unsigned short int maximum value: 65535

int minimum value: -2147483648
int maximum value: 2147483647
Unsigned int maximum value: 4294967295

long int minimum value: -9223372036854775808
long int maximum value: 9223372036854775807
Unsigned long int maximum value: 18446744073709551615
long long int minimum value: -9223372036854775808
long long int maximum value: 9223372036854775807
Unsigned long long int maximum value: 18446744073709551615

C++ Program to Find Range of Data Types

Now we will find it without using macro constants. The Range of signed data types varies from (- 2n-1) to (2n-1 -1), where n is the number of bits of data types.

For Unsigned data type minimum value is 0. So, the Unsigned data type range varies from 0 to (2n -1), where n is the number of bits of data types.

#include<iostream>
#include<math.h>
#define SIZE(x) sizeof(x) * 8
using namespace std;

// function to find the range of signed data types
void signedRange(int count)
{
  int min = pow (2, count-1);
  int max = pow (2, count-1) - 1;
  cout << min * (-1) << " to " << max << endl;
}

//function to find the range of unsigned data types
void unsignedRange(int count)
{
  unsigned int max = pow (2, count) -1;
  //Minimum value of all unsigned data type is 0
  cout << "0 to " << max << endl;
}

int main()
{
  cout << "Range of signed char: ";
  signedRange( SIZE(signed char) );

  cout << "Range of Unsigned char: ";
  unsignedRange( SIZE(unsigned char) );

  cout << "Range of signed short int: ";
  signedRange( SIZE(signed short int));

  cout << "Range of unsigned int: ";
  unsignedRange (SIZE(unsigned int));

  return 0;
}

Output:-

Range of signed char: -128 to 127
Range of Unsigned char: 0 to 255
Range of signed short int: -32768 to 32767
Range of unsigned int: 0 to 4294967295

We define a SIZE(x), which calculates the size of data types in bits. ( sizeof() calculates size in bytes so, we multiply it by 8 to convert it into bits from bytes.)

To calculate 2n and 2n-1 we use pow() function which is defined under <math.h>

Finally, we get a range of signed and unsigned data types.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *