C++:默认初始化

一、什么是默认初始化

默认初始化,顾名思义,即为在定义变量时如果没有为其指定初始化值,则该变量会被C++编译器赋予默认的值。而变量被赋予的默认值到底是什么,则取决于变量的数据类型和变量的定义位置

 

二、默认初始化的规则

规则1:内置类型的变量如果初始化,则它的默认初始化值取决于定义它的位置:

• 定义在任何函数之外的未初始化的内置类型变量(也就是全局变量)会被默认初始化为0

1 #include<iostream>
2 using namespace std;
3 int n;
4 double d;
5 int main(){
6     cout<<"int类型的全局变量的默认初始化值:"<<n<<endl;
7     cout<<"double/float类型的全局变量的默认初始化值:"<<d<<endl;
8     return 0; 
9 }

定义在函数体内部的(包括main函数)未初始化的内置类型变量(也就是局部变量)的默认初始值是未定义的(也就是一个随机数)。如果试图拷贝或以其他方式访问该变量的值,此时会引发编译错误

1 #include<iostream>
2 using namespace std;
3 int main() {
4     int n;
5     double value;
6     cout << n << " " << value << endl; //错误
7     return 0;
8 }

规则2:未初始化的内置类型的全局变量的默认初始化值还取决于变量的数据类型:

•数值数据类型的未初始化全局变量的默认初始值为0

 1 #include<iostream>
 2 using namespace std;
 3 short a;
 4 int b;
 5 long c;
 6 long long d;
 7 float e;
 8 double f;
 9 int main() {
10     cout << "short类型的默认初始值为:" << a << endl;
11     cout << "int类型的默认初始值为:" << b << endl;
12     cout << "long类型的默认初始值为:" << c << endl;
13     cout << "long long类型的默认初始值为:" << d << endl;
14     cout << "float类型的默认初始值为:" << e << endl;
15     cout << "double类型的默认初始值为:" << f << endl;
16     system("pause");
17     return 0;
18 }

•bool类型的未初始化的全局变量的默认初始化值为false(也就是0)

1 #include<iostream>
2 using namespace std;
3 bool flag;
4 int main() {
5     cout << "bool类型的默认初始值为:" << flag << endl;
6     system("pause");
7     return 0;
8 }

•char类型的未初始化的全局变量的默认初始化值为‘\0’(ASCII码值为0) 

 1 #include<iostream>
 2 using namespace std;
 3 char c;
 4 int main() {
 5     if (c == '\0') {
 6         cout << "char类型的默认初始值为\'\\0\'" << endl;
 7     }
 8     else {
 9         cout << "char类型的默认初始值不是\'\\0\'" << endl;
10     }
11     system("pause");
12     return 0;
13 }

•string类型(姑且当成内置类型)的未初始化的全局变量的默认初始值为“”

 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 string str;
 5 int main() {
 6     if (str == "") {
 7         cout << "string类型的默认初始值为\"\"" << endl;
 8     }
 9     else if(str==" ") {
10         cout << "string类型的默认初始值是\" \"" << endl;
11     }
12     else {
13         cout << "string类型的默认初始值既不是\"\",也不是\" \"" << endl;
14     }
15     system("pause");
16     return 0;
17 }

 

规则3:静态变量无论是全局变量还是局部变量,编译器都会给其默认初始化值,值为多少取决于变量的数据类型

 1 #include<iostream>
 2 using namespace std;
 3 static int value1;
 4 int main() {
 5     static int value2;
 6     cout << "全局静态变量的默认初始化值为:" << value1 << endl;
 7     cout << "局部静态变量的默认初始化值为:" << value2 << endl;
 8     system("pause");
 9     return 0;
10 }

 

规则4:指针类型的全局未初始化的变量的默认初始值为NULL,而指针类型的局部未初始化变量的默认值这是未定义的(在有些编译器下定义为初始化的局部指针变量会报错)

 1 #include<iostream>
 2 using namespace std;
 3 int* ptr;
 4 int main() {
 5     int* local_ptr; 
 6     if (ptr == NULL) {
 7         cout << "全局指针变量的默认初始值为NULL" << endl;
 8     }
 9     else {
10         cout << "全局指针变量的默认初始值不为NULL" << endl;
11     }
12     if (local_ptr == NULL) {
13         cout << "局部指针变量的默认初始值为NULL" << endl;
14     }
15     else {
16         cout << "局部指针变量的默认初始值不为NULL" << endl;
17     }
18     return 0;
19 }

 

 

PS:建议对所有的变量在其定义的时候就对其进行初始化,这样可以避免许多无意的错误

posted on 2017-08-23 00:32  Tom文星  阅读(3269)  评论(0编辑  收藏  举报

导航