struct bit fields questions in c



struct bit fields questions answers and explanation in c programming language.

(1) What will be output of c code?

#include<stdio.h>
int main() {
    struct employee {
         unsigned id: 8;
         unsigned sex:1;
         unsigned age:7;
    }; struct employee emp1={203,1,23};

    printf("%d\t%d\t%d",emp1.id,emp1.sex,emp1.age);
   
    return 0;
}


Output: 203 1 23
Explanation:
In a bit field structure, accessing its data members follows the same syntax as a regular structure, allowing for convenient interaction with individual bits.

How bit data or fields in a structure are stored in the memory:

The minimum size of a structure containing at least one bit-field member is determined by the word size of the microprocessor. The word size represents the number of bits processed in a single instruction by the microprocessor. In the context of Turbo C, which is based on the 8086 microprocessor, the word size is two bytes or 16 bits. Therefore, the smallest size a structure can occupy, due to the inclusion of at least one bit-field member, is two bytes. It's important to note that the word size may vary across different microprocessors, impacting the minimum size of structures in a programming environment.


Bits are filled from right to left direction. In the above question 8 bit is used for id, 1 bit for sex and 7 bit for age. 

(2) What will be output of c code?

#include<stdio.h>
int main() {
    struct bitfield {
         unsigned a:5;
         unsigned c:5;
         unsigned b:6;
    }bit;

    char *p;
    struct bitfield *ptr,bit1={1,3,3};

    p=&bit1;
    p++;
   
    printf("%d",*p);
    return 0;
}

Output: 12
Explanation:
Binary value of 1 is 00001 (in 5 bit)
Binary value of 3 is 00011 (in 5 bit)
Binary value of 3 is 000011 (in 6 bit)

In memory it can be represented as:


Assuming bit1 resides at memory address 500, assigned to a char pointer p, incrementing p (p++) to 501 points to the content at that address, represented as 00001100 in binary, equivalent to the decimal value 12. Therefore, the output is 12. 

(3) What will be output of c code?

#include<stdio.h>
int main() {
    struct bitfield {
         signed int a:3;
         unsigned int b:13;
         unsigned int c:1;
    }; struct bitfield bit1={2,14,1};
   
    printf("%d",sizeof(bit1));
    return 0;
}

Output: 4

(4) What will be output of c code?

#include<stdio.h>
int main() {
    struct bitfield {
         unsigned a:3;
         char b;
         unsigned c:5;
         int d;
    }bit;
   
    printf("%d",sizeof(bit));
    return 0;
}


Output: 5

Note: The actual output is 6, considering the presence of a slack byte. Before executing this program, navigate to the option menu, select compiler, go to code generation, choose word alignment, and press OK in Turbo C 3.0.

(5) What will be output of c code?

#include<stdio.h>
int main() {
    struct field {
         int a;
         char b;
    }bit; struct field bit1={5,'A'};
    char *p=&bit1;
    *p=45;
   
    printf("\n%d",bit1.a);
    return 0;
}

Output: 45

Nesting of structure:

Structures can be nested, meaning that a structure can be declared within another structure. However, to access the data members of the inner structure, it is imperative to declare an instance of the inner structure within the outer structure. Without such a declaration, direct access to the inner structure's data members is not feasible. An illustration is provided below for clarity:

(6) What will be output of c code?

#include<stdio.h>
int main() {
    struct world {
         int a;
         char b;
         struct india { char c; float d; }p;
    }; struct world st ={1,'A','i',1.8};
   
    printf("%d\t%c\t%c\t%f",st.a,st.b,st.p.c,st.p.d);
    return 0;
}

Output: 1 A I 1.800000

Array, union, structure can be member of a structure (not same structure). 

(7) What will be output of c code?

#include<stdio.h>
int main() {

    struct india {
         char c;
         float d;
    };

    struct world {
         int a[3];
         char b;
         struct india orissa;
    };

    struct world st ={{1,2,3},'P','q',1.4};

    printf("%d\t%c\t%c\t%f",st.a[1],st.b,st.orissa.c,st.orissa.d);
    return 0;
}

Output: 2 p q 1.400000

6 comments:

himanshuD said...

please explain d size of struct object in which bit fields r used

The One Thing I Do Every Day said...

i really like this post...

Unknown said...
This comment has been removed by the author.
Anonymous said...

very good

Anonymous said...

good work...

Unknown said...

Superb!!