What is the output of this C code?
int main()
{
int i = 10;
void *p = &i;
printf("%f\n", *(float*)p);
return 0;
}
Answer: D
Correct answer: 0.000000
The cast changes how the bytes are interpreted, not what they contain. The four bytes hold the integer 10, and read as an IEEE single-precision float that bit pattern is a denormal of about \( 1.4 \times 10^{-44} \) - which %f prints, to six places, as 0.000000. The program compiles because the cast is explicit; it is the meaning rather than the syntax that is wrong.
Please log in or register to report this question.