Discussion
What will be the output of the following C code?
#include <stdio.h>
void foo(float *);
int main()
{
int i = 10, *p = &i;
foo(&i);
}
void foo(float *p)
{
printf("%f\n", *p);
}
Answer: B
Correct answer: 0.000000
foo expects a float * but is given an int *. The pointer types are incompatible, so a conforming compiler must diagnose this - but the code compiles with a warning on most implementations, and the resulting behaviour is what the question asks about.
Inside foo, the four bytes holding the integer 10 are reinterpreted as a float. The bit pattern 0x0000000A read as an IEEE single is a denormal of the order of \( 10^{-44} \), which prints as 0.000000 at the default precision. The value is an accident of representation, not a conversion.
Post your comment
Please log in or register to post a comment.
Examination Categories
- SSC Exams
- Banking Exams
- Teaching Exams
- Civil Services Exams
- Railways Exams
- Engineering Recruitment Exams
- Defence Exams
- State Government Exams
- Police Exams
- Insurance Exams
- Nursing Exams
- Other Government Exams
- NRA CET
- PG Entrance Exams
- Campus Placement
- MBA Entrance Exams
- CUET and UG Entrance
- Judiciary Exams
- Regulatory Body Exams
- Software Development