6 Years
Discussion Span
Last Post by _avishek

int i;
float *pf;
pf = (float *)&i;
*pf = 100.00;
printf("\n %d", i);

i should have write this: explain the output

why not 100 is the output as pf is pointing to i and afterthat we change the value at the address pointed by pf?


You will get 100 if you change your printf() call as follows:

printf("\n%.0f", *pf);

The reason you are not getting 100 with your printf() call is essentially because computers represent floating point numbers and integers differently. The IEEE single-precision floating point format represents a floating point number as a 24-bit mantissa and an 8-bit exponent. So, (float)100.00 is represented differently from (int)100.

In your example, "i" is being used as a buffer to hold the bits for (float)100.0. Your printf() call is displaying that buffer as an integer, my printf() call is displaying that buffer as a float.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.