Discussion
Pick the best statement for the below program:
#include "stdio.h"
int main()
{
union {int i1; int i2;} myVar = {.i2 =100};
printf("%d %d",myVar.i1, myVar.i2);
return 0;
}
Answer: C
Correct answer: No compile error and it’ll print “100 100”
The two members of a union share the same storage, so writing 100 through i2 makes it visible through i1 as well - and since both are int, the bytes are interpreted the same way. The output is 100 100. (Reading a union through a member other than the one last written is only well defined when the types match like this.)
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