Why uninitialized variable print a strange negative value? [duplicate]

假如想象 提交于 2019-12-13 10:11:24

问题


Why uninitialized variable print a strange negative value ?

int x;
cout << x << endl;

回答1:


What you're doing (reading the value of an uninitialised variable) is undefined behaviour; anything can happen, from it appearing to work, to printing random values, to crashing, to buying pizza with your credit card.




回答2:


An uninitialized variable is a variable that is declared but is not set to a definite known value before it is used. It will have some value, but not a predictable one.




回答3:


When a variable is not initialized , it shows you "Garbage Value". What that mean is it can be any arbitrary number from anywhere, may be from another running application or random number from big pool of memory.



来源:https://stackoverflow.com/questions/10365453/why-uninitialized-variable-print-a-strange-negative-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!