➤ Hello World! in C++
➤ Print Number in C++
➤ Add 2 Numbers C++
➤ Arithmetic Operation
➤ Sum Avg of 3 Number
➤ Area Program in C++
➤ Simple Interest in C++
➤ Find ASCII value in C++
➤ Swap 2 Number in C++
Flow Control Programs
➤ Even-Odd in C++
➤ +ve, -ve, 0 in C++
➤ Vowel-Consonant
➤ Greatest of 3 no.
➤ Check Leap Year
➤ Calculator Program
➤ Reverse a Number
➤ Sum of Natural Number
➤ GCD of 2 Number
➤ LCM of 2 Number
➤ Find Power in C++
➤ Fibonacci Series in C++
➤ Palindrome Number
➤ Find Factorial in C++
➤ Factorial Using Recursion
➤ Prime Number in C++
➤ Prime Number b/w 1-N
Array
➤ Linear Search in C++
➤ Binary Search in C++
Others
➤ Introduction to C++
➤ Data Types in C++
➤ Range of Data Types
➤ Void main, main vs int main
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
Name | Expresses |
CHAR_MIN | Minimum value for an object of type char |
CHAR_MAX | Maximum value for an object of type char |
SCHAR_MIN | Minimum value for an object of type Signed char |
SCHAR_MAX | Maximum value for an object of type Signed char |
UCHAR_MAX | Maximum value for an object of type Unsigned char |
CHAR_BIT | Number of bits in a char object |
MB_LEN_MAX | Maximum number of bytes in a multi-byte character |
SHRT_MIN | Minimum value for an object of type short int |
SHRT_MAX | Maximum value for an object of type short int |
USHRT_MAX | Maximum value for an object of type Unsigned short int |
INT_MIN | Minimum value for an object of type int |
INT_MAX | Maximum value for an object of type int |
UINT_MAX | Maximum value for an object of type Unsigned int |
LONG_MIN | Minimum value for an object of type long int |
LONG_MAX | Maximum value for an object of type long int |
ULONG_MAX | Maximum value for an object of type Unsigned long int |
LLONG_MIN | Minimum value for an object of type long long int |
LLONG_MAX | Maximum value for an object of type long long int |
ULLONG_MAX | Maximum 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,
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 Unsigned char: 0 to 255
Range of signed short int
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!