#include <stdio.h>
int main()
{
struct value
{
int bit1:1;
int bit2:4;
int bit3:4;
} bit;
printf ("%d\n", sizeof(bit));
return 0;
}
Output on Tc/Tc++:
2
Output under Linux:
4
I know I am missing some concept of bit fields.
The
sizeoffor the struct is not the same as the sum of the sizes of all the elements – this is especially the case with bitfields.Typically, the struct needs to be padded to a certain size and alignment. (Which apparently is 2 on Tc/Tc++ and 4 in Linux.)
So although there’s only 9 bits in use, it’s being padded out to the word-size.
EDIT :
Note that the C standard does not specify how much padding is done. And therefore, you are getting different results from two different compilers.