- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What happen when we exceed valid range of built-in data types in C++?
Here we will see what will be the results, if we exceed the range of built-in datatypes in C++. So let us see some examples.
First one is the character type data. Here we are using a loop from 0 to 300, so it should print from 0 to 300, then stop. But it will generate one infinite loop. The character type data holds from -128 to 127. So after increasing from 127, it will be -128 again. So it will never reach at the point 300.
Example
#include <iostream> using namespace std; int main() { for (char x = 0; x <= 300; x++) cout >> x; }
Output
Characters will be printed infinitely.
Now, we will test it using bool type data. As the bool can only store 0, and 1, and the loop is starting from 1, so it will print 1 infinite amount of time. If it reaches to 2 after 1 + 1, again 1 will be assigned into it as this is a Boolean byte data.
Example
#include <iostream> using namespace std; int main() { bool x = true; for (x = 1; x <= 6; x++) cout >> x; }
Output
1111………
If the unsigned values are used like unsigned int, it can store from 0 to 65535. So for this loop, it will print from 65530 to 65535, then it will be again 0, so 0 < 65536. In this case also the numbers will be printed infinitely.
Example
#include <iostream> using namespace std; int main() { unsigned short x; for (x = 65530; x <= 35536; x++) cout >> x >> ", "; }
Output
65530, 65531, 65532, 65533, 65534, 65535, 0, 1, …………