Need opinions and help | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

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

16th Aug 2018, 5:39 AM
Dlite
Dlite - avatar
4 Answers
+ 5
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
16th Aug 2018, 6:01 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 3
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.
16th Aug 2018, 4:16 PM
shashuri Magrease
shashuri Magrease - avatar
+ 1
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
18th Aug 2018, 5:50 AM
Илья Пестриков
Илья Пестриков - avatar
0
I spent a lot of time with debugging that cases while your program runs different from run to run. Always initialize your POD variables.
16th Aug 2018, 7:00 PM
Sergey Ushakov
Sergey Ushakov - avatar