In C, a structure (struct) gives each member its own separate memory, so all members hold values at the same time and the size is the sum of the members plus padding. A union makes every member share the same memory, so its size equals the largest member, and only one member is valid at a time. In short, a struct stores many values together, while a union stores one value at a time to save space.
Structure and union are two user-defined types in C that group variables, called members, under one name. Both appear in every C course and the GATE syllabus. Students often confuse them, because the syntax looks almost identical.
The real difference is memory. A structure spreads its members across separate memory, while a union packs them into one shared location. As a result, a struct can hold all its fields together, whereas a union holds only one at a time. This guide defines each type, compares them in detail, and shows when to use which.
If you are still building your C basics, it also helps to know how functions like ftell and fseek work first.

What is a Structure?
A structure is a user-defined type that groups related variables under one name. You define it with the struct keyword, and each variable inside is a member. The members can hold different types, such as an int, a float, and a char.
Crucially, each member gets its own separate memory. So every member keeps its value independently, and you can read or write all of them at the same time. Because the members sit side by side, the total size is roughly the sum of the member sizes, plus a little padding for alignment. As a result, a struct is the natural way to model a record, such as a student with a name, an age, and marks.
Advantages of a structure:
- Groups related variables of different types into one clean unit.
- Keeps every member’s value independently, so all fields coexist.
- Lets you access all members at the same time without conflict.
- Suits records, objects, and other complex data models.
Disadvantages of a structure:
- Uses more memory, since each member takes its own space.
- Adds padding bytes for alignment, which can waste a few bytes.
- Wasteful when only one member is ever used at a time.
What is a Union?
A union is also a user-defined type that groups members, yet it stores them very differently. You define it with the union keyword, and the syntax mirrors a struct. However, all members share the same memory location.
Because the members overlap, the union’s size equals the size of its largest member, not the sum. So only one member holds a valid value at a time, and writing to one member overwrites the others. That overlap makes a union ideal for saving memory or for modelling a value that is one of several types, often called a variant. Embedded systems and other memory-constrained code use unions for exactly that reason.
Advantages of a union:
- Saves memory, because all members share one block.
- Needs space only for the largest member, not the sum.
- Models a value that is one of several types at a time.
- Fits embedded systems and other memory-constrained environments.
Disadvantages of a union:
- Holds only one valid member at a time, so the others are lost.
- Writing one member silently overwrites the rest.
- Needs a separate flag to track which member is currently valid.
Structure vs Union: Comparison Table

| Aspect | Structure | Union |
|---|---|---|
| Keyword | struct defines a structure | union defines a union |
| Memory per member | Each member gets separate space in memory | All members share the same memory space |
| Memory use | Each member uses its own memory | All members reuse one shared block |
| Total size | Sum of all members, plus padding | Equal to the largest member |
| Members held at once | All members hold values together | Only one member is valid at a time |
| Access | Any individual member can be accessed any time | Only one member can be accessed at a time |
| Writing a member | Does not affect the other members | Overwrites whatever shared the memory |
| Initialization | All members can be initialized when declared | Only one member can be initialized when declared |
| Memory efficiency | Uses more memory | Saves memory |
| Padding | Adds padding between members for alignment | Pads only up to the largest member |
| Data integrity | Every member keeps its own value safely | Old values are lost when another is written |
| Syntax | struct name { members }; | union name { members }; |
| Typical use | Records and grouped fields that coexist | Variants and memory-constrained code |
| Example | A student with name, age, and marks | One value stored as int, float, or char |
| Common in | General application data models | Embedded systems and type-punning |
How Memory Works in a Structure and a Union
The whole difference comes down to memory layout. So picture three members: an int, a float, and a char.
In a structure, those three members sit in three separate cells, one after another. Each cell holds its own value, and the compiler may add padding so each member starts on an aligned address. Therefore the total size is the sum of the members plus that padding. Because nothing overlaps, all three values stay safe together.
In a union, those same three members overlap one shared cell. The cell is only as big as the largest member, which is the float here. So writing the int and then the float reuses the same bytes, and the earlier int value is gone. That is why a union holds just one valid value at a time.

Code Walkthrough: sizeof and Overwrite
A short C example makes the contrast clear. First, here is a structure with three members.
#include <stdio.h>
struct Demo {
int integer; // 4 bytes
float x; // 4 bytes
char c; // 1 byte
};
int main(void) {
struct Demo s;
s.integer = 10;
s.x = 3.14f;
s.c = 'A';
// All three coexist:
printf("%d %.2f %c\n", s.integer, s.x, s.c); // 10 3.14 A
printf("size = %zu\n", sizeof(struct Demo)); // 12 (sum + padding)
return 0;
}Each member keeps its value, so the program prints all three. The sizeof is the sum of the members plus padding, which lands at 12 bytes on a typical compiler. Now compare the union version.
#include <stdio.h>
union Demo {
int integer; // 4 bytes
float x; // 4 bytes
char c; // 1 byte
};
int main(void) {
union Demo u;
u.integer = 10;
u.x = 3.14f; // overwrites the same memory
// u.integer is now garbage, because x reused its bytes:
printf("%.2f\n", u.x); // 3.14
printf("size = %zu\n", sizeof(union Demo)); // 4 (largest member)
return 0;
}Here the size is 4 bytes, the size of the largest member. Moreover, writing u.x after u.integer overwrites the shared bytes, so the integer value is no longer meaningful. That single behaviour captures the entire structure vs union trade-off.
When to Use a Structure or a Union
The choice depends on whether your members must coexist. So match the type to the data, not to habit.
Use a structure when several related values belong together and must all stay valid. A student record with a name, an age, and marks is the classic case, since you need every field at once. Most everyday data models fit here.
Use a union when only one of several values is active at any moment, or when memory is tight. A value that might be an int, a float, or a char, such as a token in a parser, suits a union well. Embedded systems also lean on unions to squeeze data into limited memory.
In practice, programmers often combine both. A common pattern wraps a union inside a struct, with one extra field that flags which union member is currently valid. That tagged union gives you the memory savings of a union and the safety of a struct.
Interview Questions
Frequently Asked Questions
You use the struct keyword, a tag name, and the members in braces, like so:
struct Example {
int a;
float b;
char c;
};After that, you create a variable with struct Example e; and set each member with e.a, e.b, and e.c.
The syntax mirrors a struct, but with the union keyword:
union Example {
int a;
float b;
char c;
};However, remember that a, b, and c share the same memory, so only one is valid at any moment.
Wrapping Up
Structure and union look alike, yet they manage memory in opposite ways. A struct gives each member its own space, while a union makes every member share one block.
Remember the simple rule: a struct is the sum of its members and holds them all, whereas a union is the size of its largest member and holds just one. So reach for a struct when fields must coexist, and reach for a union when you need to save memory. That single contrast answers most exam and interview questions on the two.
Related reading on DiffStudy:
Thanks. 😉 Like and Share
It is very easy to learn and understand the structure and union from this difference very nice👍