C Language
9. Structures, Unions & EnumsIntermediate10 min read

Structures, Unions & Enums in C

Create custom composite types using struct, optimize memory with unions, implement type aliases with typedef, and define enum constants.

Key Takeaways & Core Concepts
  • A struct groups variables of different data types together under a single user-defined type name.
  • Struct members are accessed using dot notation (student.id) or arrow notation (ptr->id).
  • A union shares a single memory location among all its members; its size equals its largest member.
  • typedef creates convenient shorthand aliases for complex types.
  • enum defines a set of named integer constants for readable state handling.

Defining and Using Structures

While arrays hold homogeneous data where all elements must be of the same type, structures allow you to group heterogeneous attributes of different data types (such as a Student having an ID, Name, and GPA) under a single cohesive entity.

Each variable inside a structure is called a member or field. Using typedef provides a clean type alias so you can declare variables without repeatedly prefixing with the struct keyword.

Defining a Custom Student Structure
1typedef struct {
2 int id;
3 char name[50];
4 float gpa;
5} Student;
6
7int main(void) {
8 Student s1 = {101, "Alice Smith", 3.85f};
9 printf("Student: %s (ID: %d) - GPA: %.2f\n", s1.name, s1.id, s1.gpa);
10 return 0;
11}
Click code or "Edit Code" to edit
Interactive Code Block•Tab = 4 spaces•Ctrl + Enter = Run
11 lines
Note: typedef struct assigns 'Student' as the type name, making variable declarations clean and readable.

Structures vs. Unions: The Memory Difference

Understanding how structures and unions allocate memory in RAM is a core systems programming concept:

In a struct, each member receives its own distinct memory offset. In a union, all members share the exact same starting memory address, which allows significant memory savings in resource-constrained embedded systems when only one attribute is active at a time.

Feature / MetricStructure (`struct`)Union (`union`)
Memory AllocationSum of all member sizes (+ compiler padding bytes)Size of the largest member only
Member AccessAll members can be stored and accessed simultaneouslyOnly one member can be actively held at any given moment
Primary Use CaseComplex entities with multiple active attributes (e.g. Student, Packet)Mutually exclusive data variants (e.g. dynamic JSON values, raw register overlays)
Keyword`struct``union`
Struct, Union, and Arrow Operator Demonstration
1#include <stdio.h>
2#include <string.h>
3
4typedef struct {
5 int id;
6 char title[32];
7 double price;
8} Course;
9
10typedef union {
11 int integerVal;
12 float floatVal;
13 char charVal;
14} VariantData;
15
16int main(void) {
17 Course c1 = {101, "C Programming Mastery", 49.99};
18 Course *ptr = &c1;
19
20 // Access via direct member (.) and pointer arrow (->)
21 printf("Course ID: %d\n", c1.id);
22 printf("Course Title: %s\n", ptr->title);
23 printf("Course Price: $%.2lf\n\n", ptr->price);
24
25 // Demonstrate Union memory sharing
26 VariantData data;
27 data.integerVal = 65;
28 printf("Union data.integerVal = %d\n", data.integerVal);
29 printf("Union data.charVal = %c (Shares same memory!)\n\n", data.charVal);
30
31 printf("Size of Course struct: %zu bytes\n", sizeof(Course));
32 printf("Size of VariantData union: %zu bytes\n", sizeof(VariantData));
33
34 return 0;
35}
Click code or "Edit Code" to edit
Interactive Code Block•Tab = 4 spaces•Ctrl + Enter = Run
35 lines
Note: In the union, setting integerVal to 65 immediately makes charVal 'A' because 65 is the ASCII code for 'A' and they share the exact same 4 bytes.
Practice & AssessmentC Programming Complete Course

Test Your Understanding

Reinforce core concepts with interactive multiple-choice questions, live coding challenges, and top technical interview questions.

Your Current Score

0 / 1 correct(0 attempted)

1

When accessing a struct member through a pointer (e.g. Student *s), which operator is used?

Beginner