问题
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