Anomalous behavior of printf()? [duplicate]
Possible Duplicate:
Using %f to print an integer variable
#include<stdio.h>
int main()
{
printf("%f",9/5);
return 0;
}
Output: 0.000000
Can anyone explain the output of the above program ?
Shouldn't be the output of program be 1.000000 ?
Solution 1:
printf("%f",9/5);
This statement is undefined behavior because the argument has to be of type double
and 9 / 5
is of type int
.
Use 9 / 5.0
to have an argument of type double
(and the correct floating division).
Solution 2:
9/5
is of type int
. Passing an int
argument to printf
with "%f"
has undefined behavior.
Try 9.0/5.0
if you want 1.800000
, or (double)(9/5)
if you want 1.000000