C Academy · Lesson

Unions and Memory Efficiency

Understand the differences between struct and union and how unions optimize memory usage.

Lesson 2 of 310 steps

Unions and Memory Efficiency is a free C Academy lesson on CoddyKit. This is lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C Academy learning path, and your progress syncs across the web and the CoddyKit app. The C Academy course includes 3 lessons in total.

Unions and Memory Efficiency

A union is a special data type that allows storing different data types in the same memory location.

In this lesson, you will learn:

  • How to define and use unions.
  • The difference between structures and unions.
  • How unions optimize memory usage.
Unions and Memory Efficiency — illustration 1

What is a Union?

A union is similar to a structure, but all members share the same memory space.

Example:

union Data { int i; float f; char str[20]; };

Here, i, f, and str share the same memory location.

Declaring and Using a Union

Unions are declared and used like structures:

union Data d1;

Assigning values:

d1.i = 10;

Only one member can store a value at a time.

Example: Using a Union

This program demonstrates how a union stores values in a shared memory location.

#include <stdio.h>

union Data {
    int i;
    float f;
    char str[20];
};

int main() {
    union Data d1;
    d1.i = 10;
    printf("Integer: %d\n", d1.i);
    d1.f = 20.5;
    printf("Float: %f\n", d1.f);
    return 0;
}

Memory Allocation in Unions

Unlike structures, a union's size is determined by its largest member.

Example:

union Data { int i; // 4 bytes float f; // 4 bytes char str[20]; // 20 bytes };

The size of this union is 20 bytes, not 28.

Difference Between Struct and Union

Structure:

  • Each member has a separate memory location.
  • Size is the sum of all members.

Union:

  • All members share the same memory location.
  • Size is determined by the largest member.

Example: Comparing Struct and Union

This program shows the memory size of a struct and a union.

#include <stdio.h>

struct StructExample {
    int i;
    float f;
    char str[20];
};

union UnionExample {
    int i;
    float f;
    char str[20];
};

int main() {
    printf("Size of struct: %lu bytes\n", sizeof(struct StructExample));
    printf("Size of union: %lu bytes\n", sizeof(union UnionExample));
    return 0;
}

When to Use Unions?

Unions are useful when:

  • Memory efficiency is a priority.
  • A variable needs to store different types at different times.
  • Working with hardware registers.

Summary

In this lesson, you learned:

  • How unions work and how they save memory.
  • The difference between structures and unions.
  • How to use unions in practical scenarios.

Next, we will explore enumerations in C!

Unions and Memory Efficiency — illustration 10
Free to start

Learn C with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
39
Lessons
144

Frequently Asked Questions

Is the “Unions and Memory Efficiency” lesson free?

Yes — the full text of “Unions and Memory Efficiency” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C Academy course, upgrade to CoddyKit PRO. The C Academy course includes 3 lessons in total.

What will I learn in “Unions and Memory Efficiency”?

Understand the differences between struct and union and how unions optimize memory usage. You practise C Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C Academy?

No prior experience is required. C Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 3.

How long does the “Unions and Memory Efficiency” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C Academy lesson?

Yes. Every C Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Defining and Using Structures
  2. Unions and Memory Efficiency
  3. Enumerations (enum)
← Back to C Academy