This is my program
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
struct bitfield {
unsigned a:3;
char b;
unsigned c:5;
int d;
}bit;
printf("%lu \n",sizeof(bit));
return 0;
}
I was expecting size of this structure to be, well quite a lot, but it comes out to be 8 on my machine, because unsigned is 4 bytes. Now reason I was expecting to be more than that was because I would expect char b
to be on a byte boundary, so that it is aligned in the memory correctly. Now my guess is that compiler is putting a, b, c, all in those 4 bytes. I am new to C, so please bear with me. Is my assumption that all the other data types besides bit fields have to be necessarily on byte incorrect? If it is correct, I would expect, a
to take the whole unsigned int, and b
to take a byte and then padding of 3 bytes, and then so on. What am I missing here?