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

Recommended Answers

All 5 Replies

Run program and see

The pioneering spirit appears to be dead, at least among the latest generation of programmers.

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

Output : 1120403456

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.