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

Discussion

What is the output of this C code?

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

 

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

Answer: B

Correct answer: 0.000000

The four bytes holding the integer 10 are read as an IEEE single-precision float, and that bit pattern is a denormal of about \( 1.4 \times 10^{-44} \), which %f prints as 0.000000. The pointer types are incompatible, so a conforming compiler must issue a diagnostic; older compilers treat it as a warning and produce this output, which is what the question is asking for.

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

Post your comment

Please log in or register to post a comment.