+ 2

Why this C code prints different output in different runs?

Shouldn't it just print 5? Cuz it converts int to double and loses the decimal numbers https://code.sololearn.com/clF1dG346S32/?ref=app

18th Jul 2021, 2:41 AM
Rishi
Rishi - avatar
2 Réponses
+ 6
Rishi Everytime you are getting garbage value because of incompatible type of int and double. So if you want to print only 5 then use %.f printf("%.f", a); Or you have to cast with int: printf("%d", (int) a);
18th Jul 2021, 2:49 AM
A͢J
A͢J - avatar
+ 1
A͢J but why does it produce a garbage value? What does it do with the a then? Won't it just misinterpret the 'a' and produce some wrong value, but related to a? (So that the output won't change everytime)
18th Jul 2021, 9:01 AM
Rishi
Rishi - avatar