How to Use Bit Field in Structure in C: Syntax and Example
In C, you use
bit fields inside a struct by specifying a colon and the number of bits after the field name, like unsigned int field:3;. This lets you store multiple small values efficiently by controlling how many bits each field uses.Syntax
A bit field in a struct is declared by adding a colon and the number of bits after the field name. For example, unsigned int age:3; means the age field uses 3 bits.
- unsigned int: type of the field (usually unsigned)
- field_name: the name of the bit field
- :number: number of bits allocated to this field
Bit fields must be of integral type and the number of bits must be less than or equal to the size of that type.
c
struct Example {
unsigned int a:3; // 3 bits
unsigned int b:5; // 5 bits
unsigned int c:1; // 1 bit
};Example
This example shows how to define a structure with bit fields and how to assign and print their values. It demonstrates saving memory by using only a few bits per field.
c
#include <stdio.h>
struct Flags {
unsigned int is_bold:1;
unsigned int is_italic:1;
unsigned int font_size:6; // up to 63
};
int main() {
struct Flags text_style = {0};
text_style.is_bold = 1;
text_style.is_italic = 0;
text_style.font_size = 12;
printf("Bold: %u\n", text_style.is_bold);
printf("Italic: %u\n", text_style.is_italic);
printf("Font size: %u\n", text_style.font_size);
return 0;
}Output
Bold: 1
Italic: 0
Font size: 12
Common Pitfalls
Common mistakes when using bit fields include:
- Using signed types can cause unexpected behavior; prefer
unsigned int. - Assigning values that exceed the bit width causes truncation.
- Bit fields cannot have their address taken, so pointers to bit fields are not allowed.
- Bit field packing and alignment can vary by compiler, so exact memory layout is not portable.
c
/* Wrong: assigning too large a value */ #include <stdio.h> struct Wrong { unsigned int small:3; }; int main() { struct Wrong w; w.small = 10; // 10 is 1010 in binary, but only 3 bits allowed printf("Value: %u\n", w.small); // Output will be truncated return 0; } /* Right: assign values within range */ #include <stdio.h> struct Right { unsigned int small:3; }; int main() { struct Right r; r.small = 5; // 5 fits in 3 bits (101) printf("Value: %u\n", r.small); return 0; }
Output
Value: 2
Value: 5
Quick Reference
Tips for using bit fields in C:
- Use
unsigned intfor predictable behavior. - Specify the exact number of bits needed to save memory.
- Remember bit fields are compiler-dependent in layout and alignment.
- Do not take the address of bit fields.
- Use bit fields for flags or small ranges of values.
Key Takeaways
Bit fields in C structs let you specify exact bit widths for fields to save memory.
Always use unsigned types and assign values within the allowed bit range to avoid truncation.
Bit field layout and alignment depend on the compiler and are not portable.
You cannot take the address of a bit field, so pointers to bit fields are not allowed.
Bit fields are ideal for storing flags or small numeric values efficiently.