- What a structure and a union actually are, in plain terms
- How each one uses memory, and why it matters
- The key differences, side by side in a table
- When to reach for one over the other
- The mistakes beginners make most often
The Difference in 30 Seconds
The short answer: a structure gives every member its own separate memory. A union makes all its members share the same memory. That single fact decides everything else between them.
If you take a structure with three members, each one lives in its own box. If you take a union with those same three members, they all sit in one box and take turns using it. Whichever member was written last is the one you get back when you read.
The rest of this page walks through what that looks like in code, how the memory is laid out, and when you would choose one over the other. If you just need the table, it is two sections below.
What is a Structure?
A structure is a way to bundle related pieces of data under one name. Think of it as a small container that holds several labelled slots, each with its own type.
For example, if you are tracking employees, you might want each employee to have a number, a name, and a salary. A structure lets you keep those three pieces together. Each employee record then has all three values stored at the same time.
Structure syntax:
struct structure_name {
data_type member1;
data_type member2;
...
};
Example — an employee structure:
struct employee {
int empno;
char ename[20];
long salary;
};
Every member of a structure has its own memory address. Changing the employee number does not touch the employee name. They live in different parts of memory. That is the whole point — a structure lets you read and write every member whenever you like.
What is a Union?
A union looks almost identical to a structure when you write it. Same syntax, same member list. The difference is what happens in memory.
In a union, all members share the same memory. The union is exactly as big as its largest member, and only one member can hold a value at a time. If you write to the name and then write to the salary, the salary overwrites the name. They occupy the same bytes.
Union syntax:
union union_name {
data_type member1;
data_type member2;
...
};
Example — the same fields as a union:
union employee {
int empno;
char ename[20];
long salary;
};
Both pieces of code look the same. But with a structure, all three values live side by side in memory, so you can read all three back whenever you want. With a union, only the most recently written member is valid. Reading the others gives you garbage.
Key Differences Between Structure and Union
If you remember nothing else from this page, remember the table below. The rest of the differences all flow from the first row.
| Feature | Structure | Union |
|---|---|---|
| Keyword | struct |
union |
| Member access | All members can be used at once | Only one member at a time |
| Memory | Each member gets its own slot | All members share one slot |
| Size | Sum of all member sizes (plus padding) | Size of the largest member |
| Changing one member | Does not affect other members | Overwrites the others |
| Initialization | Multiple members can be initialized | Only one member can hold a value |
| Typical use | Records, entities, grouped data | Variant types, saving memory |
Quick summary: Structure = separate memory, all members live at once. Union = shared memory, one member at a time.
How Memory Works in Each
Let's say you have a structure with three members: an int (4 bytes), a char array of 20 (20 bytes), and a long (8 bytes).
With a structure, the compiler reserves separate memory for each member. The total size is the sum of the parts — 4 + 20 + 8 = 32 bytes. All three members are usable at the same time, because each lives in its own address.
With a union, the compiler reserves one block of memory that is big enough for the largest member. In this case, the largest is the 20-byte array, so the union is 20 bytes. All members are mapped to that same starting address. If you write to the int and then write to the long, the long overwrites the int. You cannot hold both at once.
One small detail: the size of a structure is sometimes larger than the sum of its members. This is because of padding — the compiler adds a few unused bytes between members to keep each one aligned to a natural boundary. The union does not have this issue, because only one member is valid at a time.
When to Use Structure vs Union
This is the question most beginners ask first, and it is the right question. The answer comes down to one thing: do you need to keep all the values at the same time, or just one at a time?
Reach for a structure when:
- You have several related fields that must all be accessible at once. Think of an employee record, a student mark sheet, or a point with x and y coordinates.
- You are modelling a real object. Most objects in the real world have several attributes that coexist.
- You do not care about saving every last byte. Structures are the normal, predictable choice.
Reach for a union when:
- You are short on memory, and you know only one value will be used at a time. Embedded systems and microcontrollers are the classic cases.
- You are parsing a network packet where the meaning of the same bytes depends on a preceding tag. This is the "tagged union" pattern you see in C libraries.
- You want to peek at the raw bytes of one type as another type. Not something you do often, but it has its uses when debugging binary formats.
In everyday C code, structures are the default. You will use them dozens of times per project. Unions are more of a special-purpose tool — you will use them when you need them and forget about them otherwise.
Common Mistakes Beginners Make
These are the errors I see most often when students run this code for the first time. Check your own code against each one before you move on.
- Reading a union member you did not write last. If you assign to the
intand then read thechararray, you get nonsense. That is not a bug. That is how unions work. Only the last written member is valid. - Expecting the size of a structure to be exactly the sum of its members. Padding can add a few bytes. Use
sizeofif you need the exact size. Do not add up the members in your head. - Using a union when a structure was needed. If you find yourself confused about why a value keeps changing, check whether you accidentally used a union. Most of the time you wanted a structure.
- Initializing more than one union member at once. This will not compile. A union holds exactly one member at a time, so it can be initialized with exactly one value.
- Assuming
sizeof(struct)equalssizeof(union)when both have the same members. They never do. The struct is larger. That is the whole point.
Full Program Example
Here is a complete program that shows both a structure and a union with the same members. Run it and watch the sizes come out different.
Practice Exercise
Challenge: Write a program that defines a structure and a union with the same three members, then prints the size of each using sizeof.
Click to see the solution
#include <stdio.h>
struct EmpStruct {
int id;
char name[20];
float salary;
};
union EmpUnion {
int id;
char name[20];
float salary;
};
int main() {
printf("Size of Structure: %lu bytes\n", sizeof(struct EmpStruct));
printf("Size of Union: %lu bytes\n", sizeof(union EmpUnion));
return 0;
}
/* Sample Output:
Size of Structure: 28 bytes
Size of Union: 20 bytes
*/
Frequently Asked Questions
1. What is the main difference between structure and union?
Memory allocation. A structure gives each member its own separate memory, so the size of the structure is the sum of all members. A union gives all members the same shared memory, so its size is just the size of the largest member.
2. Can I access all members of a union at the same time?
No. In a union, only one member can hold a value at a time. If you assign a new value to a different member, it overwrites the previous one because all members share the same memory location.
3. When should I use a union instead of a structure?
Use a union when you need to save memory and only one member will be used at a time. Use a structure when you need to store multiple values at the same time. The union is especially useful in embedded systems and network protocols where memory is tight.
4. How is the size of a structure calculated?
The size of a structure is the sum of the sizes of all its members, plus any padding the compiler adds for alignment. This is why the sizeof of a structure can be larger than you expect.
5. Can a structure contain a union and vice versa?
Yes. You can nest a union inside a structure and a structure inside a union. This is common when you want to store a type tag alongside the value, such as in a variant type or a tagged union.
Quick rule: Use a structure when you need to store and access multiple values. Use a union when you need to save memory and only one value is needed at a time.