C++ global and local variables

前端 未结 8 890
遇见更好的自我
遇见更好的自我 2021-01-16 03:26

I have encountered the following code:

#include
using namespace std;
int i = 1;
int main(int argc,char ** argv)
{
    int i = i;
    cout&l         


        
8条回答
  •  逝去的感伤
    2021-01-16 03:44

    int main()
    {
        int i=i;
        return 0;
    }
    

    is correct.

    So in your program, the global i is ignored when the local i is encountered and it is initialized to itself. You'll get a garbage value as a result.

提交回复
热议问题