|
发表于 2017-7-6 16:41:41
|
显示全部楼层
本楼为最佳答案
用C语言举个例子吧
- #include <stdio.h>
- struct student
- {
- int a, b, c, d;
- } little;
- int x;
- int main(void)
- {
- printf("a: %d\n", little.a);
- printf("b: %d\n", little.b);
- printf("c: %d\n", little.c);
- printf("d: %d\n", little.d);
- printf("x: %d\n", x);
-
- return 0;
- }
复制代码
- a: 0
- b: 0
- c: 0
- d: 0
- x: 0
- --------------------------------
- Process exited after 0.2191 seconds with return value 0
- 请按任意键继续. . .
复制代码
全局变量如果没有给初始值,就初始化为0,没有疑问吧
那么
- class student
- {
- public:
- int a, b, c, d;
- } little;
复制代码
这不就是声明并定义了一个全局的 little吗
第二个
- #include <stdio.h>
- struct student
- {
- int a, b, c, d;
- };
- int main(void)
- {
- struct student little;
- int x;
- printf("a: %d\n", little.a);
- printf("b: %d\n", little.b);
- printf("c: %d\n", little.c);
- printf("d: %d\n", little.d);
- printf("x: %d\n", x);
- return 0;
- }
复制代码
编译时有警告
- 1>c:\visualstudioprojects\tmp\tmp\main.c(14): warning C4700: 使用了未初始化的局部变量“little”
- 1>c:\visualstudioprojects\tmp\tmp\main.c(18): warning C4700: 使用了未初始化的局部变量“x”
复制代码
很明显,没有初始化局部变量
在Visual Studio 2017中运行时报错
点忽略后可以输出
- a: -858993460
- b: -858993460
- c: -858993460
- d: -858993460
- x: -858993460
- 请按任意键继续. . .
复制代码
你的第二张截图不用说了吧,明显就是定义了一个局部变量,没有初始化
|
|