converting from int to float in C changes value

c, casting

Solution

You have to say that in your format string. Use:

fprintf(stderr, "%d,%f\n", rgbValues->green, (float)rgbValues->green);
                     ^

instead of:

fprintf(stderr, "%d,%d\n", rgbValues->green, (float)rgbValues->green);
                     ^

Note the change from `d` to `f` (the circumflex `^` isn't part of the code, just an indicator as to where to look).

Problem

Hi I'm trying to convert from an `int` to a `float` in C and for some reason the cast changes the value and I'm not sure why. So: ``` fprintf (stderr, "%d,%d\n", rgbValues->green, (float)rgbValues->green); ``` produces two different numbers. Note that `rgbValues->green` is an `int`. Any idea why this is happening? Thanks

Original source