In a program of sum of two input number in c . When i give only one input it assign automatically second no by 8.why? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

In a program of sum of two input number in c . When i give only one input it assign automatically second no by 8.why?

https://code.sololearn.com/c2WCFiU4jgNh/?ref=app

5th Nov 2018, 12:03 PM
Roshan Kumar
Roshan Kumar - avatar
2 Answers
+ 3
This occurs because with you declare a variable and don't initialize. The variable have a garbage value because you are using a space in memory that already have something. Why is 8? I do no this is undefined behavior. Just initialize your variables before do anything. It's good practice and prevents to happen bugs that can happen with garbages values Edit: You can see more of this effect if you don't pass anything in your code. For me printed 0 and 8. You can think 0 happens because if you pass nothing it will set to 0 and i say nops it's really undefined behavior.
5th Nov 2018, 12:12 PM
Anya
Anya - avatar
+ 3
Anya is right..Another good reason to initialize your pointers as "nullptr" if you dont have a use for them beforehand....
5th Nov 2018, 12:34 PM
Mensch
Mensch - avatar