Discussion
What is the size of the following Union ? Assume that the size of int = 2, size of float = 4, size of char = 1
union tag {
int a;
float b;
char c;
};
Answer: B
All members of a union share the same memory, so its size is that of its largest member. Here the members are an int of 2 bytes, a float of 4 bytes and a char of 1 byte, so the largest is 4 bytes.
A size of 2 or 1 is smaller than the float, and 7 is the sum of the sizes, which is the size of a structure and not of a union, if padding is ignored. Option B is correct. The compiler allocates the space of the largest member to the union, and the members take turns to use it, so only one of them is valid at a time.
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