asit said:
#include <stdio.h>
//#include <strlen.h>
union xxx
{
char name[10];
double d;
int ff;
};
int main()
{
union xxx s;
printf("%d\n",sizeof(s));
getch();
return 0;
}
why o/p is 16 in GCC compiler ??
Assuming for the moment that printing a `size_t` through an
`int`-printing format doesn't shift you into an alternate
reality [1], it's because a `union xxx` object has size 16.
It's got to have size at least 10, because of the `name`
field. And it's got to be an integer multiple of the size
of `double` and `int` if your implementation needs to
align those types properly for efficiency. Let's suppose
we can ignore the `int` in favour of the `double` for now.
If double's size were 2, 5, or 10 there'd not need to be
extra padding to get the alignment right. If it were 3 or 4
or 6, we'd only need to pad to 12 bytes; if it were 7,
we'd only need to pad to 14 bytes.
8, however, requires padding to 16 bytes. I expect that
sizeof double = 16 on your implementation and that's your
seeing the padding requirement.
[1] Watch out for Cybermen, earpods, and nine-bit bytes.