Discussion
What is the output of this C code?
#include <stdio.h>
int main()
{
int i = 10;
void *p = &i;
printf("%f\n", *(float*)p);
return 0;
}
Answer: D
Correct answer: 0.000000
The cast is on the pointer this time, so the compiler accepts it and reads the four bytes holding the integer 10 as though they were an IEEE single-precision float.
The bit pattern 0x0000000A interpreted that way is a subnormal number of about \( 1.4 \times 10^{-44} \), which %f prints to six decimal places as 0.000000. Nothing is converted - the same bits are simply given a different meaning, which is what type punning through a pointer does.
No comment is present. Be the first to comment.
Loading…
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