当我们超出C++内置数据类型的有效范围时会发生什么
我们都知道,在C/C++或任何其他编程语言中学习数据类型都是很重要的。因为我们在编写代码和作为软件工程师的职业生涯中一直都在使用它们。
每种数据类型都与特定的大小和内存相关联,当超出其范围时,它会成为一个无限循环,但永远不会达到其应有的值。在这里,我们将看到char,int,bool等的所有示例。
C++代码
// 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
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;
}
输出:
‑ !"#$%&'()*+,-./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++代码
// 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
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;
}
输出:
/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++ 代码
// 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
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;
}
输出:
111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111............(infinite loop)
C++代码
// 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
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;
}
输出:
32767
-32768
-32767 ...............(infinite loop)
C++ 代码
// 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
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;
}
输出:
65532
65533
65534
65535
0
1
2
3
4...............(infinite loop)