+ 1
Why do we use sum=0 while finding out a sum of the integers.
Please tell
3 Answers
+ 3
That's to initialize the variable to which you will add the integers. If you don't, it will start with a garbage value not corresponding to anything.
+ 2
Something to add:
If it is initialized outside of main, as a global variable, it will be initialized to a guaranteed value, depending on what type it is. For example, int's are initialized to zero by default in this case.
It will, however, as Zen has said, initialize as random garbage if it is initialized locally.
0
Thanks bro