Limits in C++

Before using the data types, one should know the maximum and minimum value of the same. It helps to avoid the numeric overflow error. In this program, you will get to know the maximum and minimum values of integer data types. These are short, int, and long as described in the data type in C++

This program uses the climits header file. The climits header file contains all the constants like SHRT_MAX, INT_MAX, etc. This file provides the value of maximum and minimum of short, int, and long integers. Each of these is signed integer types. To define the unsigned integer type, we need to declare it separately. As char are also integer data types, this file contains the required data for the char data.

The following table illustrates the constants of the climits file, their values, and their description. These values may vary depending on the values stored in the file.

ConstantsValueDescription
sizeof(short)2Size of short int
sizeof(int)4Size of int
sizeof(long)4Size of long int
SHRT_MAX32767Maximum short value
SHRT_MIN-32768Minimum short value
USHRT_MAX65535Maximum unsigned short value
INT_MAX2147483647Maximum int value
INT_MIN-2147483648Minimum int value
UINT_MAX4294967295Maximum unsigned int value
LONG_MAX2147483647Maximum long value
LONG_MIN-2147483648Minimum long value
ULONG_MIN4294967295Maximum unsigned long value
CHAR_BIT8Number of bits in a char
CHAR_MAX127Maximum char value
CHAR_MIN-128Minimum char value
UCHAR_MAX255Maximum unsigned char value

The size of the operator is used to get the size of the data types. This program shows that the size of short is 2 bytes, int is 4 bytes and long is 4 bytes. The size of char is 1 byte i.e. it contains 8 bits. We can get the size of the short, int, long, or char by using the function sizeof() on the particular data types. Please go through the program to get the proper insight into the functionality.

Program

#include<iostream>
#include<climits>
int main(){
    using namespace std;
    
    short short_max = SHRT_MAX;
    int int_max = INT_MAX;
    long long_max = LONG_MAX;
    
    short short_min = SHRT_MIN;
    int int_min = INT_MIN;
    long long_min = LONG_MIN;
    
    unsigned ushort_max = USHRT_MAX;
    unsigned uint_max = UINT_MAX;
    unsigned ulong_max = ULONG_MAX;
    
    int char_bits = CHAR_BIT;
    int char_max = CHAR_MAX;
    int char_min = CHAR_MIN;
    int uchar_max = UCHAR_MAX;
    
    cout<<"size of short is "<<sizeof(short)<<" bytes"<<endl;
    cout<<"size of int is "<<sizeof(int)<<" bytes"<<endl;
    cout<<"size of long is "<<sizeof(long)<<" bytes"<<endl;
    
    cout<<"maximum value of short is "<<short_max<<endl;
    cout<<"maximum value of int is "<<int_max<<endl;
    cout<<"maximum value of long is "<<long_max<<endl;
    
    cout<<"minimum value of short is "<<short_min<<endl;
    cout<<"minimum value of int is "<<int_min<<endl;
    cout<<"minimum value of long is "<<long_min<<endl;
    
    cout<<"maximum value of unsigned short is "<<ushort_max<<endl;
    cout<<"maximum value of unsigned int is "<<uint_max<<endl;
    cout<<"maximum value of unsigned long is "<<ulong_max<<endl;
    
    cout<<"number of bits in a char is "<<char_bits<<endl;
    cout<<"maximum value of char is "<<char_max<<endl;
    cout<<"minimum value of char is "<<char_min<<endl;
    cout<<"maximum value of unsigned char is "<<uchar_max<<endl;
    
    return 0;
}

You can copy the program in any text editor and save it as integer_limit.cpp. The same can be viewed in the image below. Here Dev C++ IDE is used for writing, compiling and running the program. Image 1 highlights the program and image 2 shows the result for the same.

Program for getting the minimum and maximum value of integer data types.
Output of the above program.

You might have noted that there is not unsigned short, unsigned int or unsigned long. All of these have the value 0. They are also not assigned in the climits file. If you force it, you will get a compile-time error. The unsigned is used to store the large positive values. We should use unsigned integer types for the values which are always positive. We should use these types according to the usage. short is smaller than an int, so it can be used to conserve memory. Thought you might not have any shortage of memory in your computer but it is really helpful if we are using an embedded system.

Leave a Reply