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.
#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;
}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.
union Value { int i; double d; char c; };
printf("%lu\n", sizeof(union Value)); // size of the largest (double)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).
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.
#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;
}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 enum | With enum |
|---|---|
if (state == 2) | if (state == PAUSED) |
| What does 2 mean? | Reads like plain English |
| Easy to mix up numbers | Compiler 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
intunderneath.
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
enumgives 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?
What is the difference between a union and a structure?
What is an enum in C?
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?
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?
int and a double takes the size of the double, since that is the larger of the two.