As explained in another answer, padding is used to facilitate struct (and union) member alignment and is the reason for the unexpected size that you see.
To see the size you expected (i.e. without padding), you can force the code to to ignore the alignment issue by using a pragma
statement. #pragma pack
specifically is used to indicate to the compiler that the struct
/union
be packed in a specific way, by changing the addressing alignment from the default value to something else.
The following results in a size of 28:
#pragma pack(1) //Sets compiler to align on 1 byte address boundaries
//(Effectively removing all padding)
union U {
int x[7];
double y[3];
}z;
#pragma pack() //Set packing back to what it was before
int main(){
printf("%d",sizeof(z));
return 0;
}