The %d
format specifier says to printf
, "take the next 4 bytes off of the stack, interpret them as an integer, and print out that integer." Since you're actually passing a float
as a parameter, the bytes of the float
(which are stored in IEEE-754 format) are getting misinterpreted as an integer, hence the different values. (Actually, the float
is getting converted to a double
due to argument promotion within variadic functions, and it's the first 4 bytes of that promoted double
that are getting interpreted as an integer.)
The correct solution is to use one of the %e
, %f
, or %g
format specifiers instead of %d
when printing out a float
. %e
says, "take the next 8 bytes off the stack, interpret them as a double
, and print it out using scientific (exponential) notation" %f
prints out using a fixed-point format, and %g
prints out whichever would be shorter of %e
or %f
.
fprintf(stderr, "%d,%f\n", rgbValues->green, (float)rgbValues->green);