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’ :

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

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’ :

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

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.



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

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 -32767 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. -32767) 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’ :

// 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 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.

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.



My Personal Notes arrow_drop_up

Improved By : abhinavaggarwal36




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

Recommended Posts:



1.9 Average Difficulty : 1.9/5.0
Based on 35 vote(s)






User Actions