What happen when we exceed valid range of built-in data types in C++?

Consider the below programs.
1) Program to show what happens when we cross range of ‘char’ :

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


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

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


This code will print ‘1’ infinite time because here ‘a’ is declared as ‘bool’ and it’s 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 with in the loop. See this for Bool data type.

3) Program to show what happens when we cross 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.



filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Will this code print ‘a’ till it becomes 32770? Well the answer is 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 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.

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Will this code print ‘a’ till it becomes 65536? Well the answer is 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 computer uses 2’s complement to represent data. For example if we have 1 byte (We can use char and use %d as format specifier to view it as decimal), we can represent -128 to 127. If we add 1 to 127 we will get -128. Thats 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.

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 first 8 bits, so that’s 0. Hence we get 0 after adding 1 in 255.

This article is contributed by Aditya Rakhecha . If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@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.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :


81


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.