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
// 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:
‑ !"#$%&'()*+,-./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
// 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:
/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
// 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:
111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111............(infinite loop)
C++ Code
// 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:
32767
-32768
-32767 ...............(infinite loop)
C++ Code
// 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:
65532
65533
65534
65535
0
1
2
3
4...............(infinite loop)