0Pricing
C Academy · Lesson

Unions and Memory Efficiency

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

1

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

2

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.

All lessons in this course

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