What happens to uninitialized variables? C++

前端 未结 3 1547
北海茫月
北海茫月 2020-11-29 12:58
int main()
{    
    int a;
    cout << a;
    return 0;
}

I am wondering why the value 0 is being output. I thought if a variable is uniniti

3条回答
  •  萌比男神i
    2020-11-29 13:25

    Well the reason being, a variable gets garbage value( a value unknown/senseless to program) is when someone runs a program, it gets loaded in some part of RAM. Now it all depends what values were previously set to certain location, may be some other program was there previously. It just happen the your program has loaded into a that location where it happens to be 0 value in RAM and that's what you are getting in return.

    It quite possible that if restart your system and try running the same program then you might get garbage value.

    Above statements are valid for variables which doesn't get initialized by the compiler.

提交回复
热议问题