Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does printf print an integer as a double? [duplicate]

Tags:

c

printf

printf("%f", 1.0); //prints 1.0

but

printf("%f", 1);  // prints 0.0

How did the conversion happen?

like image 313
redkont Avatar asked Dec 23 '22 18:12

redkont


1 Answers

printf("%f", 1); causes undefined behavior, because double is expected, but you passed an int. There is no explanation of why it prints 0.0, because the behavior is undefined.

like image 85
Ayxan Haqverdili Avatar answered Dec 28 '22 08:12

Ayxan Haqverdili