Structures vs. Unions in C: A Practical Guide
What is a Structure?
A structure is a user‑defined data type in C that groups logically related data items of different types into a single unit. Each member occupies its own contiguous memory location, so a structure can hold multiple values simultaneously.
Example: a student record that stores a name, roll number, and marks under one variable name.
What is a Union?
A union is also a user‑defined type, but all members share the same memory space. The union’s size equals the size of its largest member. This feature makes unions ideal for memory‑constrained scenarios where only one member is needed at a time.
Declaring a Structure
struct Student {
char name[60];
int roll_no;
float marks;
};
Notice the use of the struct keyword and the semicolon after the closing brace.
Example: Using a Structure
#include <stdio.h>
struct Student {
char name[60];
int roll_no;
float marks;
} sdt;
int main(void) {
printf("Enter the following information:\n");
printf("Student name: ");
fgets(sdt.name, sizeof(sdt.name), stdin);
printf("Roll number: ");
scanf("%d", &sdt.roll_no);
printf("Marks: ");
scanf("%f", &sdt.marks);
printf("\nEntered data:\n");
printf("Name: %s", sdt.name);
printf("Roll number: %d\n", sdt.roll_no);
printf("Marks: %.1f\n", sdt.marks);
return 0;
}
Output (example):
Enter the following information: Student name: James Roll number: 21 Marks: 67 Entered data: Name: James Roll number: 21 Marks: 67.0
Declaring a Union
union Item {
int x;
float y;
char ch;
};
Example: Using a Union
#include <stdio.h>
union Item {
int x;
float y;
char ch;
};
int main(void) {
union Item it;
it.x = 12;
it.y = 20.2f;
it.ch = 'a';
printf("x = %d\n", it.x);
printf("y = %f\n", it.y);
printf("ch = %c\n", it.ch);
return 0;
}
Because all members share memory, setting ch overwrites the previous values of x and y. Only the last stored value is valid.
Structure vs. Union
| Feature | Structure | Union |
|---|---|---|
| Declaration keyword | struct | union |
| Memory layout | Each member gets its own space | All members share one space |
| Changing one member | Does not affect others | Overwrites other members |
| Initialization | All members can be initialized simultaneously | Only the first member can be initialized at a time |
| Total size | Sum of all member sizes | Size of the largest member |
| Typical use | Store related data of various types | Store one of several possible types |
| Access pattern | Any member can be accessed at any time | Only one member is valid at a time |
| Flexible array support | Supported | Not supported |
When to Use Each
- Structure: When you need to maintain multiple related fields simultaneously, such as a record of a student or an employee.
- Union: When you have a value that could be of several types but only one at a time, such as a variant type or a memory‑constrained sensor value.
Pros and Cons
Structure Advantages
- Groups related data under a single name, improving readability.
- Easy to pass entire records to functions.
- Arrays of structures enable storage of many records.
- Supports flexible arrays for variable‑length data.
Structure Disadvantages
- Requires more memory, as every member is stored.
- Changes to the structure definition propagate throughout the code.
- Can become unwieldy in large projects with deeply nested structures.
Union Advantages
- Minimal memory footprint: only the largest member’s size.
- Useful for implementing type tags or variant data.
- Can simplify code that uses a single value of varying type.
Union Disadvantages
- Only one member can hold a valid value at any time.
- Harder to debug due to shared memory.
- Cannot use flexible arrays.
C Language
- C++ Struct vs Class: Key Differences, Syntax, and Best Use Cases
- C vs. C++: Key Differences & When to Choose Each
- C vs Java: A Comprehensive Comparison of Features, History, and Applications
- Python vs JavaScript: Key Differences, Features, and When to Choose Each
- Python vs Ruby: A Comprehensive Comparison of Features, Advantages, and Use Cases
- Joining vs Welding: Key Differences Explained
- Welding vs Brazing: Key Differences Explained for Better Metal Joining
- Up vs Down Milling: Key Differences Explained
- Soldering vs. Brazing: Understanding the Critical Differences
- Key Differences Between Ships and Boats Explained