+ 2

C - Why does this code, without casting x to int, return a garbage value?

float x = 1.99; printf("%d",x);

26th Sep 2019, 8:59 PM
Paolo De Nictolis
Paolo De Nictolis - avatar
1 Answer
+ 1
It is not garbage, numbers with decimal points are stored in different way than integers and when you try to print them in different type it fails. So, you have to cast it to get right value.
26th Sep 2019, 9:13 PM
Irma MasleĆĄa
Irma MasleĆĄa - avatar