In my program below I can understand why the short
members of the union are stored in the same 2 bytes (as size of the union is that of the largest member).But I just don't get it why all 4 character members are stored in the same byte while one would assume they would be spread across two bytes and stored like
c1,c3--->first byte
c2,c4--->2nd byte
Or more clearly
Byte1 Byte2
----short a----
----short b----
--c1-- --c2--
--c3-- --c4--
What is the reason they are stored instead as
Byte1 Byte2
----short a----
----short b----
--c1--
--c2--
--c3--
--c4--
What is the reason behind this?Her's the program:
#include<stdio.h>
int main(void)
{
union test
{
short a,b;
char c1,c2,c3,c4;
} var= {65};
printf("%hd,%hd,%c,%c,%c,%c",var.a,var.b,var.c1,var.c2,var.c3,var.c4);
}
Result:
65,65,A,A,A,A