Search code examples
cbit-fields

Bitfields in C without variable name


What is the use of unnamed bit-fields in C?

Example :

typedef struct fun {
unsigned int                 :8;
unsigned int foo1            :1;
unsigned int foo2            :1;
unsigned int foo3            :1;
unsigned int foo4            :1;
unsigned int foo5            :1;
}dig;

What is the use of line :

   unsigned int                 :8;

Solution

  • It's called unnamed bit field. C11 standard states about it and possible usage:

    6.7.2.1 Structure and union specifiers

    A bit-field declaration with no declarator, but only a colon and a width, indicates an unnamed bit-field. 106) As a special case, a bit-field structure member with a width of 0 indicates that no further bit-field is to be packed into the unit in which the previous bitfeld, if any, was placed.

    106) An unnamed bit-field structure member is useful for padding to conform to externally imposed layouts.