The output of this program is 28. I don’t understand how?
According to me this should be 32(4+4+4+4+12)+4(to maintain the alignment)=32.
Please explain the reason for displaying the output 28??
struct test{
char c;
int d;
int x;
int y;
long double p;
}t1;
printf("%d",sizeof(t1));
Maybe your “long double” is actually the same as a double (8 bytes), and if you’re on a 32bit processor the alignment is 4-byte.
4+4+4+4+8 = 24
What is
sizeof(long double)?EDIT:
I used GCC’s
__builtin_offset_of()and__alignof__to investigate. The actual answer that explains the size of the struct is:4+4+4+4+12 = 28
sizeof(long double)is 12.No padding is necessary because
__alignof__(long double)is 4 and. Interestingly,__alignof__(double)is 8.