如果在函数体外定义变量,我们没有给它初始值,那么变量会被默认初始化。
//例如:
#include<iostream>
using namespace std;
int a;
string b;
int main(){
cout<<a<<endl;
cout<<b<<endl;
}
输出结果:
0
在函数体内定义变量时不赋初始值,不会被默认初始化,就会报错。
#include<iostream>
using namespace std;
int main() {
int c;
string d;
cout << c << endl;
cout << d << endl;
}
结果直接报错: