Machine-translated by Google. Questions are authored in English.
Top

Discussion

What is the output of this C code?

int main()
{
int i = 10;
void *p = &i;
printf("%f\n", *(float*)p);
return 0;
}

 

  • A.Compile time error
  • B.Undefined behaviour
  • C.10
  • D.0.000000

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.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.