【发布时间】:2015-08-03 09:43:21
【问题描述】:
考虑以下代码:
#include "stdafx.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int count123;
for (int c = 0; c < 10; c++)
{
count123 += c;
}
return 0;
}
编译时我收到警告:warning C4700: uninitialized local variable 'count123' used
我知道 reason 正在声明 count123 但没有初始化它。
但是,如果我将 count123 声明为如下代码中的全局变量,警告就会消失。
#include "stdafx.h"
using namespace std;
int count123;
int _tmain(int argc, _TCHAR* argv[])
{
for (int c = 0; c < 10; c++)
{
count123 += c;
}
return 0;
}
据我所知,将 count123 声明为全局变量会改变其范围,但如何消除警告?请指导。
【问题讨论】:
标签: c++ global-variables