paizza paizza - 1 month ago 9
C++ Question

Which "value" has an un-init value?

I've this code:

double val1;
double val2 = 0.0;

if(val1 != val2) {
cout << "different: " << val1 << " | " << val2 << endl;
}


which enter to the
if-statement
. I think that
val1
doesn't get 0.0 by default (i.e. NULL). Which value does it get so? I always thought 0.0 was by default...

Answer

Local non-static variables that are not initialized will have an indeterminate value, and it will be seemingly random.

You should not use uninitialized local variables as that leads to undefined behavior.