Can anybody explain the code? Output is 4 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can anybody explain the code? Output is 4

#include <stdio.h> union u { int a; char c; }u1; int main() { printf("%d",sizeof(u1)); return 0; }

25th Apr 2020, 8:04 AM
Ravindra Raghuwanshi
2 Answers
25th Apr 2020, 8:44 AM
Mustafa K.
Mustafa K. - avatar
+ 1
A union allocates a single memory location with the largest data type size inside the union and is shared by all the other members inside it. So when compared to char, int has larger in size and hence the answer 4.
25th Apr 2020, 8:45 AM
Avinesh
Avinesh - avatar