Javatpoint Logo
Javatpoint Logo

Bit Fields in C

In C language, we have union and struct data types where we can declare user-defined data types. The size of the struct depends on data members. But sometimes, we do not need such a huge size of the data type, because it occupies memory, and it creates a waste of memory.

Example 1:

Output:

Bit Fields in C

Explanation:

In the above example, we have a struct data type which has three data members: date, month and year of integer type. This data type will represent the date of birth, and three integers will occupy 12 bytes of memory. But we need not store these values in huge memory since we know the maximum value of date can be 31 and month can be 12, and year can be of maximum digits.

So we use the bit fields to save the memory. In the bit field, we can explicitly give the width or the range to the data member in terms of bytes.

Syntax:

Example 2:

Output:

Bit Fields in C

Explanation:

In the above code, we have used the bit field in the struct variable since we know that we can have a maximum 31 value in the day variable which can be represented using 5 bits, so we forcefully used five as width.

In the same way, we need only 12 values as the maximum in the month section, so we can represent it with a maximum of 4 bits (the maximum value is 15 with 4 bits).

For the year, we used 12 bits as its width.

So then we calculated the whole memory occupied by the struct variable then we got 4 bytes which were initially 12 bytes. So, with the help of bit fields, we have saved 8 bytes of memory.

Note: If we give the width to a data member more than its actual range, then it will give the compilation error.

Example 3:

Output:

Bit Fields in C

Explanation:

In the above code, we have a character data type which occupies 1 byte or 8 bits of memory generally. But in the bit field section, we gave it a width of 12 bits which exceeds its range, and it will give a compilation error.

Note: We cannot use the bit field concept in the array.

Example 4:

Output:

Bit Fields in C

Explanation:

In the above, we have an integer array as a data member of the structure, and we applied the bit field, so we got the compilation error.

Note: We can use data members without any name with 0 widths to take the memory jump and assign the memory to the following data member after the memory jump.

Example 5:

Output:

Bit Fields in C

Explanation:

In the above code, we have one character member and then an integer member without any name with 0 widths. So we will take the jump of 4 bytes then the next character data member will occupy the memory.

Note: We cannot get the address of the data member if we use bit fields.

Example 6:

Output:

Bit Fields in C

Explanation:

We got the error, if we got the address of the bit field data member.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA