I executed the following code
#include <stdio.h>
int main()
{
printf("%f\n", 9/5);
}
Output : 0.000000
why not 1 ?
if i write printf("%f %f %d %d\n", (float)9/5, 4, sizeof(float), sizeof(int));
then output is 1.800000 0.000000 4 59
why not 1.800000 4 4 4
on my machine the sizeof (float) is 4
Thanks in advance
This is because your
printfformat specifier doesn’t match what you passed it:9/5is of typeint. Butprintfexpects afloat.So you need to either cast it to a float or make either literal a float:
As for why you’re getting
0.0, it’s because theprintf()is reading the binary representation of1(an integer) and printing it as afloat. Which happens to be a small denormalized value that is very close to0.0.EDIT : There’s also something going with type-promotion on varargs.
In vararg functions,
floatis promoted todouble. Soprintf()in this case actually expects a 64-bit parameter holding adouble. But you only passed it a 32-bit operand so it’s actually reading an extra 32-bits from the stack (which happens to be zero in this case) – even more undefined behavior.