The result of the multiplication of a float
and an int
is a float
. Besides that, it will get promoted to double
when passing to printf
. You need a %a
, %e
, %f
or %g
format. The %d
format is used to print int
types.
Editorial note: The return value of main
should be int
. Here's a fixed program:
#include <stdio.h>
int main(void)
{
float a = 3.3f;
int b = 2;
printf("%a\n", a * b);
printf("%e\n", a * b);
printf("%f\n", a * b);
printf("%g\n", a * b);
return 0;
}
and its output:
$ ./example
0x1.a66666p+2
6.600000e+00
6.600000
6.6
printf
is a varargs method, with signatureint printf ( const char * format, ... )
. This means that it takes arbitrary arguments after the first, and it cannot check them at compile time or cast them to a desired type taken from the format string. Your format string says%d
, which looks for anint
. The expressiona*b
isfloat
, which gets converted todouble
. These types don't even have the same memory size, so the temporarydouble
gets physically cut in half and treated as anint
. This is not a Solomonic solution. – Fortyniner