Structures, Unions, Enums, typedef, and Bit Fields
Model richer data in C using user-defined types and understand layout-oriented constructs used in systems and embedded programming.
Inside this chapter
- Structures
- Enums and typedef
- Unions
- Bit Fields
- Nested and Self-Referential Structures
- Real-World Usage Snapshot
Series navigation
Study the chapters in order for the clearest path from C basics to advanced memory, systems, debugging, and real-world development practice. Use the navigation at the bottom of each page to move smoothly through the full tutorial.
Structures
struct Student {
int id;
char name[50];
float marks;
};
Structures group related fields into one logical type. They are widely used for records, configuration, packets, nodes, and domain-like data in C programs.
Enums and typedef
typedef enum {
LOW,
MEDIUM,
HIGH
} Priority;
enum improves readability by naming integer states. typedef creates clearer type aliases, which is especially helpful in larger programs.
Unions
A union allows different members to share the same memory location. This is useful when only one representation is needed at a time, such as certain protocol, device, or low-level optimization scenarios.
Bit Fields
Bit fields allow compact representation of flags inside structures. They are common in hardware interfaces and memory-sensitive structures, though they need careful use because layout behavior can be implementation-dependent.
Nested and Self-Referential Structures
struct Node {
int data;
struct Node *next;
};
Self-referential structures are the foundation of linked lists, trees, graphs, and many low-level data models.
Real-World Usage Snapshot
User-defined types make C suitable for meaningful system design, not just small calculations. Structs and enums appear in network packets, configuration systems, file metadata, embedded registers, and domain-specific in-memory models.