Why do we use sum=0 while finding out a sum of the integers. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why do we use sum=0 while finding out a sum of the integers.

Please tell

22nd Oct 2016, 4:13 PM
rahul sharma
rahul sharma - avatar
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.
22nd Oct 2016, 4:32 PM
Zen
Zen - avatar
+ 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.
22nd Oct 2016, 6:11 PM
Zeke Williams
Zeke Williams - avatar
0
Thanks bro
22nd Oct 2016, 4:33 PM
rahul sharma
rahul sharma - avatar