Skip to content
Related Articles

Related Articles

What Happen When We Exceed Valid Range of Built-in Data Types in C++?

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 16 May, 2022

In this article, we will look at what happened when we exceed the valid range of built-in data types in C++ with some examples.

Example 1: Program to show what happens when we cross the range of ‘char’.

Here, a is declared as char. Here the loop is working from 0 to 225. So, it should print from 0 to 225, then stop. But it will generate an infinite loop. The reason for this is the valid range of character data is -128 to 127. When ‘a’ become 128 through a++, the range is exceeded and as a result, the first number from the negative side of the range (i.e. -128) gets assigned to a. As a result of this ‘a’ will never reach point 225. so it will print the infinite series of characters.

CPP




// C++ program to demonstrate
// the problem with 'char'
#include <iostream>
  
using namespace std;
  
int main()
{
    for (char a = 0; a <= 225; a++)
        cout << a;
    return 0;
}

This code will print ‘1’ infinite time because here ‘a’ is declared as ‘bool’ and its valid range is 0 to 1. And for a Boolean variable, anything else than 0 is 1 (or true). When ‘a’ tries to become 2 (through a++), 1 gets assigned to ‘a’. The condition a<=5 is satisfied and the control remains within the loop.

Example 2: Program to show what happens when we cross the range of ‘bool’ 

CPP




// C++ program to demonstrate
// the problem with 'bool'
#include <iostream>
  
using namespace std;
  
int main()
{
    // declaring Boolean
    // variable with true value
    bool a = true;
  
    for (a = 1; a <= 5; a++)
        cout << a;
  
    return 0;
}

Will this code print ‘a’ till it becomes 32770? Well, the answer is an indefinite loop because here ‘a’ is declared as a short and its valid range is -32768 to +32767. When ‘a’ tries to become 32768 through a++, the range is exceeded and as a result, the first number from the negative side of the range(i.e. -32768) gets assigned to a. Hence the condition “a < 32770” is satisfied and control remains within the loop.

Example 3: Program to show what happens when we cross the range of ‘short’ 

Note that short is short for short int. They are synonymous. short, short int, signed short, and signed short int are all the same data type. 

CPP




// C++ program to demonstrate
// the problem with 'short'
#include <iostream>
  
using namespace std;
  
int main()
{
    // declaring short variable
    short a;
  
    for (a = 32767; a < 32770; a++)
        cout << a << "\n";
  
    return 0;
}

Example 4: Program to show what happens when we cross the range of ‘unsigned short’

CPP




// C++ program to demonstrate
// the problem with 'unsigned short'
#include <iostream>
  
using namespace std;
  
int main()
{
    unsigned short a;
  
    for (a = 65532; a < 65536; a++)
        cout << a << "\n";
  
    return 0;
}

Will this code print ‘a’ till it becomes 65536? Well, the answer is an indefinite loop, because here ‘a’ is declared as a short and its valid range is 0 to +65535. When ‘a’ tries to become 65536 through a++, the range is exceeded and as a result, the first number from the range(i.e. 0) gets assigned to a. Hence the condition “a < 65536” is satisfied and control remains within the loop.

Explanation: We know that the computer uses 2’s complement to represent data. For example, if we have 1 byte (We can use char and use %d as a format specifier to view it as a decimal), we can represent -128 to 127. If we add 1 to 127 we will get -128. That’s because 127 is 01111111 in binary. And if we add 1 into 01111111 we will get 10000000. 10000000 is -128 in 2’s complement form. The same will happen if we use unsigned integers. 255 is 11111111 when we add 1 to 11111111 we will get 100000000. But we are using only the first 8 bits, so that’s 0. Hence we get 0 after adding 1 in 255.

This article is contributed by Aditya Rakhecha and improved by Sakshi Tiwari If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!