Getting Started With C

Bitfields in C are a feature that allows you to represent data structures with custom-sized bit fields within a struct. This can be particularly useful when memory is a concern, and you want to pack multiple fields together more efficiently. Bitfields are defined using a colon : followed by the number of bits each field should occupy.

  • Examples :

    Program.c


  • Output:

                Flag 1: 1
                Flag 2: 0
                Flag 3: 1

    Resources :

    Test Your Knowledge
    Choose The

    With supporting text below as a natural lead-in to additional content.

    Q1.

    Consider the following bit-field declaration in C:
                                struct Flags {
                                    unsigned int flag1 : 1;
                                    unsigned int flag2 : 3;
                                };
                                What is the size (in bits) of the entire structure Flags?
                            

    Q2. What is the primary advantage of using bit fields in C?