Discussion
Predict the output of the program below. Assume that the size of an integer is 4 bytes and size of character is 1 byte. Also assume that there is no alignment needed.
union test
{
int x;
char arr[8];
int y;
};
int main()
{
printf("%d", sizeof(union test));
return 0;
}
Answer: C
Correct answer: 8
A union is as large as its largest member. Here int x and int y are 4 bytes each, and char arr[8] is 8 - so the union is 8 bytes. That the two ints are separate members makes no difference: all three overlay the same storage.
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