What Happens When We Exceed Valid Range Of Built In Data Types In C++

We all know that learning data types in C/C++ or any other programming language, for that matter, is essential. As keep using them all the time in our coding and career as software engineer journey.

Every data type will be associated with a specific size and memory, and when it exceeds its range, it turns out to be an infinite loop but will never reach the value it is supposed to be. Here we will see all the examples of char, int, bool etc.

C++ Code

Example

// Here we are writing down the C++ programming language code to demonstrate 
// the concept What Happens When We Exceed Valid Range of Built-in Data Types 
// in C++? and C++ program to demonstrate the problem with 'char'
#include <iostream>
using namespace std;
// The main Driver Code Functionality starts from here
int main()
{
    // A simple for loop code
	for (char b = 1; b <= 100; b++)
		cout << b;
	return 0;
}

Output:

Output

‑ !"#$%&'()*+,-./0 1 2 3 4 5 6 7 8 9 : ; < = > ? @  A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ ` a b c d

C++ Code

Example

// Here we are writing down the C++ programming language code to demonstrate 
// the concept What Happens When We Exceed Valid Range of Built-in Data Types 
// in C++? and C++ program to demonstrate the problem with 'char'
#include <iostream>
using namespace std;
// The main Driver Code Functionality starts from here
int main()
{
    // A simple for loop code
	for (char b = 1; b <= 1225; b++)
		cout << b;
	return 0;
}

Output:

Output

/tmp/7q8zRNkkRt.o
‑ !"#$%&'()*+,-./0123456789:;<=>? @ A B CD E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ ` a b c d e f g h I j k l m n o p q r s t u v w x y z { | } ~ �� � � � � � � � � � � � � � � �� � � � � � � � � � � � � � � � �� � � � � � � � � � � � � � � � � � � � � �

C++ Code

Example

// Here we are writing down the C++ programming language code to demonstrate 
// the concept What Happens When We Exceed Valid Range of Built-in Data Types 
// in C++? and C++ program to demonstrate the problem with 'bool'
#include <iostream>
using namespace std;
// The main Driver Code Functionality starts from here
int main() {
	// the below code snippet declares Boolean
	// variable with true value
	bool a = true;
              // A simple for loop code
	for (a = 1; a <= 5; a++)
		cout << a;

	return 0;
}

Output:

Output

111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111............(infinite loop)

C++ Code

Example

// Here we are writing down the C++ programming language code to demonstrate 
// the concept What Happens When We Exceed Valid Range of Built-in Data Types 
// in C++? and C++ program to demonstrate the problem with 'bool'
#include <iostream>
using namespace std;
// The main Driver Code Functionality starts from here
int main()
{
	// the below small code snippet helps us with declaring a short variable
	short a;
    // A simple for loop code
	for (a = 32767; a < 32770; a++)
		cout << a << "\n";

	return 0;
}

Output:

Output

32767
-32768
-32767 ...............(infinite loop)

C++ Code

Example

// Here we are writing down the C++ programming language code to demonstrate 
// the concept What Happens When We Exceed Valid Range of Built-in Data Types 
// in C++? and C++ program to demonstrate the problem with 'unassigned short'
#include <iostream>
using namespace std;
// The main Driver Code Functionality starts from here
int main()
{
	unsigned short a;
    // A simple for loop code
	for (a = 65532; a < 65536; a++)
		cout << a << "\n";

	return 0;
}

Output:

Output

65532
65533
65534
65535
0
1
2
3
4...............(infinite loop)

Input Required

This code uses input(). Please provide values below: