Difference Between Structure and Union in C Language
What is a Structure?
A struct groups variables of different data types under one name, with each member having its own memory space.
struct Student {
int id;
char name[20];
};
What is a Union?
A union also groups variables of different data types under one name, but all members share the same memory location.
union Data {
int i;
float f;
};
Key Differences
- Memory: struct allocates separate memory for each member; union allocates memory equal to its largest member, shared by all.
- Access: in a struct, all members can hold valid values simultaneously; in a union, only one member holds a valid value at a time.
- Size:
sizeof(struct)is the sum of all members' sizes (plus padding);sizeof(union)equals its largest member's size.
Proving the Difference With a Program
Printing sizes and overwriting members makes the memory behaviour visible. Compile and run this on your own machine.
#include <stdio.h>
struct Record {
int id; /* usually 4 bytes */
char grade; /* 1 byte, followed by padding */
double marks; /* usually 8 bytes */
};
union Value {
int i;
float f;
char text[8];
};
int main(void) {
struct Record r = {101, 'A', 87.5};
union Value v;
printf("struct size: %zu\n", sizeof(struct Record));
printf("union size: %zu\n", sizeof(union Value));
printf("%d %c %.1f\n", r.id, r.grade, r.marks); /* all three valid */
v.i = 65;
printf("v.i = %d\n", v.i);
v.f = 3.5f; /* reuses the same bytes */
printf("v.f = %.1f\n", v.f);
printf("v.i = %d\n", v.i); /* no longer 65 */
return 0;
}
On a typical 64-bit system the struct reports 16 bytes and the union 8 bytes. Writing v.f replaced the bytes that held v.i, so reading v.i afterwards prints a large unrelated number.
Why the Struct Is Bigger Than Its Members
The members of Record add up to 13 bytes, yet sizeof says 16. The compiler inserts padding so that the double starts at an address that is a multiple of its alignment, which lets the processor read it efficiently. Member order affects padding, so placing larger members first often shrinks a struct. For example, char a; double b; char c; commonly takes 24 bytes, while double b; char a; char c; takes 16.
A union also respects alignment. Its size is at least the size of its largest member, rounded up if needed to satisfy the strictest alignment among its members.
The Tagged Union Pattern
Because a union does not remember which member was written last, real programs pair it with a tag that records the active type. This pattern appears in interpreters, message parsers and configuration systems.
enum Kind { INT_VAL, FLOAT_VAL };
struct Number {
enum Kind kind;
union {
int i;
float f;
} as;
};
void print_number(struct Number n) {
if (n.kind == INT_VAL)
printf("%d\n", n.as.i);
else
printf("%f\n", n.as.f);
}
struct Number a = { INT_VAL, { .i = 42 } };
struct Number b = { FLOAT_VAL, { .f = 2.5f } };
The designated initializers .i and .f pick which member to set. Without them, a union initializer always sets the first member.
Choosing One and Avoiding Errors
| Situation | Better choice |
|---|---|
| A record whose fields are all needed together, like a student or invoice | Structure |
| A value that can be one of several types, but only one at a time | Union inside a struct with a tag |
| Viewing the same hardware register or packet bytes in two ways | Union, with care about byte order |
| Memory-tight embedded code holding mutually exclusive data | Union |
- Do not read a union member you did not write last unless you deliberately want to reinterpret the bytes.
- Do not assume sizeof a struct equals the sum of its members; always use
sizeof. - Do not write struct or union bytes directly to files or networks expecting another compiler or machine to read them identically, since padding and byte order can differ.
- Do add a tag field whenever a union can hold different types over time.
Ready to master real-world C Programming development?
Learn C Programming hands-on with mentor-led, live sessions.