Need opinions and help | Sololearn: Learn to code for FREE!


Need opinions and help

I'm not much of a C++ programmer, so I find it difficult to understand this. #include <iostream> using namespace std; int main(){ int x,y,sum; sum = x+y; std::cout << sum; return 0; } It outputs 8, despite not assigning values to either x or y

8/16/2018 5:39:44 AM


4 Answers

New Answer


Yahiko for static variable, default value is zero... whatever you mentioned are not static variable and hence it might have any garbage value... here on sololearn, you are getting some value... that value you may or may not get on other compiler... to conclude, uninitialised non static variable may have any garbage value and it's not safe to access value of same before value assignment


I will advise you initialize all your variables you plan to use to zero. Because when you declare these variables, they are given a temporal memory address which could contain any value and those values can significantly affect your program.


Just initialize all your variables with zero and you will have no problems. All your new variables get its memory address and therefore the value that is kept there. So you need to change this initial value in the way you need in the program


I spent a lot of time with debugging that cases while your program runs different from run to run. Always initialize your POD variables.