Integer data limits checking – Computer Sir Ki Class Code Learning #CPP#2122    ## Integer data limits checking

A program to check and understand integer data type data limits

## Learning Objectives

• Understanding the 4 byte integer data type maximum and minimum value.

## Source Code

 #include using namespace std; int main( ) { int imax=2147483647; int imin=-2147483648; int imaxe=2147483648; int imine=-2147483649; cout<<"Integer max should be 2147483647"<

## Run Output

``````Integer max should be 2147483647
Integer min should be -2147483648

Attempted to print 2147483647
Actually printed :2147483647
Attempted to print -2147483648
Actually printed :-2147483648
Attempted to print 2147483648
Actually printed :-2147483648
Attempted to print -2147483649
Actually printed :2147483647``````

## Code Understanding

int imax=2147483647; int imin=-2147483648;
These two declaration and initialisation are for correct maximum and minimum integer data limits for 32 bit (4 Bytes) integer data type. A simple int on most modern computers if of 4 byte size while on old DOS based compilers it is of 2 bytes size.

int imaxe=2147483648; int imine=-2147483649;
these declarations are for erroneous data value. Notice that last number is 1 more for the positive side of integer and 1 less for the negative side integer limits.

cout<<“Integer max should be 2147483647″<<endl;
cout<<“Integer min should be -2147483648″<<endl<<endl;
These two lines have been given to display the allowed values, so that they are available on the display screen for comparison purposes.

cout<<“Attempted to print 2147483647″<<endl;
cout<<“Actually printed :”<<imax<<endl;
cout<<“Attempted to print -2147483648″<<endl;
cout<<“Actually printed :”<<imin<<endl;
With these instructions correct values are printed as the values given are in the range.

cout<<“Attempted to print 2147483648″<<endl;
cout<<“Actually printed :”<<imaxe<<endl;
cout<<“Attempted to print -2147483649″<<endl;
cout<<“Actually printed :”<<imine<<endl;
These instructions print the incorrect results as the values are out of the range.

## Notes

• The maximum and minimum int size in terms of powers of 2 is  (2^31-1) and (2^31). This is because half the range is on positive side and half on negative side. For unsigned integer the entire positive side of values are available from 2^31-1 as the maximum value.

## Common Errors

• Since large figurative symbols are there in the program, it can go wrong with very small human mistakes. One has to be extra careful in writing figures.

Suggested Filename(s): int-data-limits.cpp

sunmitra| Created: 2-Jan-2018 | Updated: 28-Aug-2018|

×