C++全局变量和局部变量

twl*_*yao 1 c++ variables scope

我遇到了以下代码:

#include<iostream>
using namespace std;
int i = 1;
int main(int argc,char ** argv)
{
    int i = i;
    cout<<i<<endl; // which i?
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

它可以通过编译,但给出错误的答案,如何解释这个?

Mic*_*urr 6

int i = i;在声明中main()宣称,隐藏全局变量的局部变量.

它自己初始化(具有不确定的值).所以全球i根本就没用过.