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;
}
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 a infinite loop. The reason for this is the valid range of character datatype 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. As a result of this ‘a’ will never reach at point 225. so it will print the infinite series of character.
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 -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’ :
// 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.
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.