Sizeof BitFields in Structure

A bitfield is a struct member that allows the user to specify how many bits it should occupy in memory. The sizeof operator in C returns the size of a variable or data type in bytes. When applied to a bitfield, sizeof returns the size of the data type that the bitfield is a member of, not the size of the bitfield itself.

Here's an example to illustrate this:

#include <stdio.h>

struct example 
{
    unsigned int a : 4;
    unsigned int b : 28;
    unsigned long long c : 64;
};

int main() 
{
    struct example ex;

    printf("Size of example struct: %lu bytes\n", sizeof(ex));
    printf("Size of a: %lu bytes\n", sizeof(ex.a));
    printf("Size of b: %lu bytes\n", sizeof(ex.b));
    printf("Size of c: %lu bytes\n", sizeof(ex.c));

    return 0;
}

In this example,  a struct is explained with three bitfields a, b, and c. a has 4 bits, b has 28 bits, and c has 64 bits.

When we use the sizeof operator on the example struct, it returns the size of the entire struct in bytes. In this case, the size of the struct would be the sum of the sizes of a, b, and c, which is 16 bytes (128 bits).

When we use the sizeof operator on each individual bitfield, it returns the size of the data type that the bitfield is a member of. In this case, a and b are of type unsigned int, which is typically 4 bytes on most modern systems. However, c is of type unsigned long long, which is typically 8 bytes on most modern systems.

So, in this case, sizeof(ex.a) and sizeof(ex.b) would return 4 (bytes), while sizeof(ex.c) would return 8 (bytes).

Note that the exact size of unsigned int and unsigned long long may vary depending on the system you're running on, but they are typically at least 32 bits and 64 bits respectively.