I am declaring a union data type inside a struct (but two different syntax). I get different output for the size of the structs. What's the catch here?
union u {
double x;
int y;
};
union {
double x;
int y;
} u_1;
struct s1 {
int a;
union {
int b;
double c;
};
};
struct s2 {
int a;
union u{
int b;
double c;
};
};
int main()
{
u u1;
s1 s_1;
s2 s_2;
cout<< sizeof(u1)<< " "<<sizeof(u_1)<<" " <<sizeof(s_1)<<" " <<sizeof(s_2);
}
I expected the output: 8 8 16 16 but the actual output is 8 8 16 4.