Day#95 C' Coding Challenge

Day#95 C' Coding Challenge

What are bit fields? How to use bit fields in structure declaration?

-------------------------------------

We know that variables in C can be bool, integer, char, long and so on. The minimum space is required for character or a bool which requires 1 byte. While the character requires all bits in a byte, bool requires only 1 bit and the remaining 7-bits in bool are stuffed and of no use which meant memory is wasted. C programming has also a way to use a memory efficiently in such scenarios. This is possible while declaring a structure. 

For example, let us see the below structure declaration.

struct
{
    int rollno;
}student;

The above structure occupies more space than below structure.

struct
{
    int rollno:1;
}student;

Advantages of bit fields are:
  • Easy to implement
  • Saves memory
The same has been explained in the below video:

Post a Comment

0 Comments