What Happens When We Exceed Valid Range Of Built In Data Types In C++ - C++ Programming Tutorial
C++ Course / Miscellaneous / What Happens When We Exceed Valid Range Of Built In Data Types In C++

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

BLUF: Mastering What Happens When We Exceed Valid Range Of Built In Data Types In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: What Happens When We Exceed Valid Range Of Built In Data Types In C++

C++ is renowned for its efficiency. Learn how What Happens When We Exceed Valid Range Of Built In Data Types In C++ enables low-level control and high-performance computing in the tutorial below.

Understanding data types in C/C++ or any programming language is crucial. We frequently utilize them in our coding endeavors and throughout our careers as software engineers.

Every data type is assigned a particular size and memory allocation. If a value goes beyond its designated range, it can result in an endless loop without ever reaching the intended value. In the upcoming examples, we will explore various instances involving char, int, bool, and other data types.

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:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience