How does C++ proceed for += with a variable without value? -
this question has answer here:
- why uninitialized variable print in c++? 4 answers
i learning c++ , had question "weird" things noticed while testing have learnt.
so use :
int marc[9]; int sum = 0; (int x =0; x < 9; x++) { marc[x] = x*4; sum += marc[x]; } cout << sum << endl;
and normal result 144
however, if changed
int sum = 0;
to
int sum;
i got crazy result 19557555002
so wondering happening in second case?
thanks answer :)
you operating on uninitialized memory in second case. non-static local variables not initialized 0 runtime in other languages, if need sum
have defined value, must initialize yourself. 19557555002 integer interpretation of bytes present @ memory address allocated sum
.
further reading: what happens declared, uninitialized variable in c? have value?
Comments
Post a Comment