Union and structure are two important user-defined data types in C. At first glance they look similar because both let you group different data members under one name. But the way memory is allocated for them is very different, and that difference changes how they should be used in real programs.
If you understand this topic clearly, you can avoid memory bugs, reduce memory usage where needed, and choose the right data type for the right problem. In this article, we will compare unions and structures in C in a simple way, with syntax, memory explanation, size comparison, examples, use cases, and common mistakes.
What is the Difference Between Union and Structure in C?
The main difference between a structure and a union in C is memory allocation.
In a structure, every member gets its own memory. In a union, all members share the same memory location.
This means a structure can store values for all of its members at the same time, while a union can safely hold one active member value at a time.
- structure: separate storage for each member
- union: shared storage for all members
Structure in C
A structure in C is used to combine different variables under one name. Each member inside the structure has its own memory, so all member values exist together.
Syntax:
struct Student {
int roll;
float marks;
char grade;
};Example:
#include <stdio.h>
struct Student {
int roll;
float marks;
char grade;
};
int main(void) {
struct Student s = {101, 88.5f, 'A'};
printf("Roll = %d\n", s.roll);
printf("Marks = %.1f\n", s.marks);
printf("Grade = %c\n", s.grade);
return 0;
}Here all three members hold their values at the same time because each member has separate storage.
Union in C
A union in C also groups different members under one name, but unlike a structure, all members use the same memory location. The union size is based on its largest member, with alignment rules also affecting the final result on some systems.
Syntax:
union Data {
int i;
float f;
char ch;
};Example:
#include <stdio.h>
union Data {
int i;
float f;
char ch;
};
int main(void) {
union Data d;
d.i = 25;
printf("i = %d\n", d.i);
d.f = 7.5f;
printf("f = %.1f\n", d.f);
return 0;
}When d.f is assigned, the old value stored through d.i is overwritten because both members share the same memory area.
Memory Difference Between Union and Structure in C
This is the most important section of the comparison.
- In a structure, memory is allocated separately for every member.
- In a union, one shared memory block is allocated, and all members use that same block.
- Because of this, a structure can hold all values together, while a union should be treated as holding one active value at a time.
Suppose we define the following members:
int number;
float price;
char grade;In a structure, enough memory is reserved for all three members. In a union, memory is reserved only for the largest member, then that same area is reused for every member.
Size Difference Between Union and Structure in C
The size of a structure is generally the sum of its members plus possible padding for alignment. The size of a union is generally the size of its largest member, possibly rounded to meet alignment requirements.
Example program:
#include <stdio.h>
struct ExampleStruct {
int i;
float f;
char ch;
};
union ExampleUnion {
int i;
float f;
char ch;
};
int main(void) {
printf("Size of structure = %zu\n", sizeof(struct ExampleStruct));
printf("Size of union = %zu\n", sizeof(union ExampleUnion));
return 0;
}On many systems, the structure size may become 12 bytes because of padding, while the union size may become 4 bytes because the largest member is 4 bytes. But exact output can vary depending on compiler, target architecture, and alignment rules.
Member Access in Union and Structure
The syntax for accessing members is similar in both cases. If the variable is not a pointer, use the dot operator. If the variable is a pointer, use the arrow operator.
variable.memberpointer->member
The difference is not in the syntax. The difference is in what the memory means after you write a value.
#include <stdio.h>
union Value {
int i;
float f;
};
int main(void) {
union Value v;
v.i = 65;
printf("i = %d\n", v.i);
v.f = 9.25f;
printf("f = %.2f\n", v.f);
printf("Reading i again = %d\n", v.i);
return 0;
}The last printed integer is not the original 65. It is the integer interpretation of the bytes currently stored for the float value. This is why a union must be used carefully.
When to Use Structure in C
Use a structure when you need all members to hold valid values together.
- student records
- employee data
- coordinates like x, y, z
- configuration data with multiple fields
- embedded data packets where different fields must be kept together
A structure is the correct choice when the members describe one object and all fields matter at the same time.
When to Use Union in C
Use a union when different members represent alternative views of the same memory or when only one member value is active at a time.
- memory-sensitive embedded systems
- protocol parsing where a byte buffer may be viewed in different ways
- device register access
- tagged data models where a separate type field tells which union member is valid
- low-level system programming
In real projects, unions are often paired with an enum so the program knows which member currently contains valid data.
enum ValueType {
TYPE_INT,
TYPE_FLOAT
};
struct TaggedValue {
enum ValueType type;
union {
int i;
float f;
} data;
};This pattern is much safer than using a raw union without tracking the active member.
Union vs Structure in C Table
| Feature | Structure | Union |
|---|---|---|
| Memory allocation | Separate memory for each member | One shared memory block for all members |
| Size | Sum of members plus possible padding | Size of largest member plus possible alignment effect |
| Values stored | All member values can exist together | Only one member value should be treated as active |
| Memory efficiency | Lower than union | Higher when members are alternatives |
| Typical use | Records and grouped data | Shared storage and alternate data views |
| Risk level | Usually easier and safer for beginners | Needs careful handling to avoid invalid reads |
Common Mistakes in Unions vs Structures in C
- using a union when all member values are needed at the same time
- reading a different union member than the one most recently written
- assuming structure size is always the exact sum of member sizes
- ignoring padding and alignment when calculating memory layout
- forgetting to track the active union member in larger programs
These mistakes often produce confusing output because the code compiles, but the memory behavior is misunderstood.
Best Practices for Unions vs Structures in C
- choose a structure when you need all fields together
- choose a union only when shared storage is intentional
- use
sizeofinstead of guessing structure or union size - pair unions with an enum or status field when practical
- document clearly which union member is active
- test low-level code on the actual target platform if memory layout matters
FAQs
Which is better in C, union or structure?
Neither is better in every case. A structure is better when you need all members at the same time. A union is better when members are alternatives and memory saving matters.
Can a union and structure have the same members?
Yes. The member list can look the same, but the memory behavior is different. A structure stores all members separately, while a union shares one memory area among them.
Why is union size smaller than structure size?
A union usually needs memory only for its largest member, while a structure usually needs memory for all members. Alignment rules can also affect the final size.
Can all union members contain valid values together?
No. Since all union members share the same memory, writing one member overwrites the byte pattern used by the others. In normal use, only one member should be considered active at a time.