#include <stdio.h>
int main(int argc, const char *argv[])
{
int a;
printf("%d",a);
return 0;
}
这段代码是c代码,编译通过,运行时输出 是一个 负数,说明 c语言在定义变量时,变量已经被初始化为默认值。
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <string.h>
#include <stdio.h>
using namespace std;
int main(int argc, const char *argv[])
{
int b;
int &a;
int c=10;
b=10;
printf("%d",b);
return 0;
}
这是c++代码,如果注释掉 第二行,输出时 与c代码是一个值,但是编译不通过,是因为a 对象的引用,假设如果不被初始化,a会像c语言那样赋值一个巨大的负数,c++为了避免这种不安全的情况出现,要求必须初始化,同理const 对象时 也必须初始化。