#include <stdio.h>
int main() {
typedef struct {
unsigned int a : 1;
unsigned char b : 1;
} bf;
printf("%ld\n", sizeof(bf));
return 0;
}
It is a simple code about C struct bit field example,
I expected to get 4
but my VisualStudio says it is 8
.
Otherwise, according to the compiler website, www.onlinegdb.com, I can get 4
as I expected.
so I think my VisualStudio has given me the wrong answer, what am I missing??
+) I have changed the example code,
unsigned int a : 3; unsigned char b : 1;
to
'unsigned int a : 1; unsigned char b : 1;
because people misundertand that I think sizeof
gives 'bits', not 'byte', so I changed it.
I think unsigned int a
has 4 bytes already, so unsigned char b
can use that already occupied space together.
In the case, I was supposed to get 4
but VisualStudio says 8
, that means they use different spaces