🟡 Intermediate  ·  Lesson 28

Unions & Enumerations

Two handy tools: union and enum

This lesson covers two smaller but useful C features: the union, which lets several members share one memory space, and the enum, which gives readable names to numbers. Both are simple once you see them in action.

What is a union?

A union looks like a structure, but with one big difference: all its members share the same memory. Only one member holds a real value at a time — writing to one overwrites the others.

C Language
#include <stdio.h>
union Value {
    int i;
    float f;
};
int main() {
    union Value v;
    v.i = 10;
    printf("i = %d\n", v.i);
    v.f = 3.14;              // overwrites the same memory
    printf("f = %.2f\n", v.f);
    return 0;
}
Output:
i = 10
f = 3.14

How a union shares memory

Because members overlap, a union is only as big as its largest member — not the sum.

C Language
union Value { int i; double d; char c; };
printf("%lu\n", sizeof(union Value));   // size of the largest (double)
Output:
8

The union takes 8 bytes — the size of the double — even though it has three members, because they all live in the same space.

When unions are useful

  • Storing a value that could be one of several types, but only one at a time.
  • Saving memory in tight, embedded or low-level programs.
  • Interpreting the same bytes in two ways (advanced, low-level use).
💡 Struct vs union in one line

A structure holds all its members at once; a union holds one at a time. For the full comparison see structure vs union in C.

Enumerations (enum)

An enum gives names to a list of related integer constants, so your code reads like words instead of numbers.

C Language
#include <stdio.h>
enum Day { MON, TUE, WED, THU, FRI };   // 0,1,2,3,4
int main() {
    enum Day today = WED;
    printf("Day number: %d\n", today);   // 2
    if (today == WED) printf("Midweek!\n");
    return 0;
}
Output:
Day number: 2
Midweek!

By default the first name is 0 and each next one is one higher. You can also set values explicitly: enum Status { OK = 1, FAIL = 5 };.

Why enum beats plain numbers

Without enumWith enum
if (state == 2)if (state == PAUSED)
What does 2 mean?Reads like plain English
Easy to mix up numbersCompiler assigns values

Common mistakes

  • Reading a union member you did not last write — you get garbage.
  • Expecting a union to hold all members at once (that is a structure).
  • Assuming enum values always start at 1 — they start at 0 by default.
  • Forgetting that an enum is really just an int underneath.
🏋️ Practice

Make a union that stores either an int or a char, and print its sizeof. Then define an enum Color { RED, GREEN, BLUE } and print the number for GREEN.

Summary

  • A union's members share one memory space; only one is valid at a time.
  • A union's size equals its largest member.
  • Use unions when a value is one of several types, one at a time.
  • An enum gives readable names to integer constants, starting at 0.
  • enum makes code clearer than bare numbers; see structure vs union for the full struct/union comparison.

Frequently Asked Questions

What is a union in C?
A union is a user-defined type whose members all share the same block of memory, so only one member holds a valid value at any moment. Its size equals that of its largest member. Unions are useful when a value could be one of several types but only one at a time.
What is the difference between a union and a structure?
In a structure every member has its own separate memory, so all members can hold values at once and the size is the sum of all members. In a union all members overlap in one shared space, so only one is valid at a time and the size is that of the largest member. See the structure vs union lesson for a full comparison.
What is an enum in C?
An enum (enumeration) is a type that gives friendly names to a set of integer constants, such as enum Day { MON, TUE, WED };. By default the names start at 0 and increase by one, so MON is 0, TUE is 1, and so on, making code far more readable than bare numbers.
Why use an enum instead of numbers?
An enum makes code self-explaining: writing if (day == SUNDAY) is much clearer than if (day == 0). It also groups related constants under one type and lets the compiler assign the values for you, reducing mistakes and making the code easier to change later.
How much memory does a union use?
A union uses only as much memory as its largest member, because all members share the same space. For example a union containing an int and a double takes the size of the double, since that is the larger of the two.
← Back to C Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.