A Union in C programming is a user-defined derived data type that allows different data types to be stored in the exact same memory location. While syntactically similar to structures (using the union keyword instead of struct), a union differs fundamentally in how memory is allocated: in a structure, each member possesses its own dedicated memory space, whereas all members of a union share a single, common memory block.
union union_name
{
data_type member1;
data_type member2;
data_type member3;
};
Size of a Union: The compiler allocates memory equivalent to the size of its largest member (with padding for memory alignment). At any given moment in time, only one member can hold a meaningful value.
Overwriting: Assigning a value to one member immediately overwrites the data stored in all other members.
Feature |
Structure ( |
Union ( |
|---|---|---|
Keyword |
|
|
Memory Allocation | Each member gets separate memory. | All members share the same memory. |
Total Size | ≥ Sum of sizes of all members. | Equal to size of largest member. |
Value Access | All members accessible simultaneously. | Only one member accessible at a time. |
Initialization | All members can be initialized together. | Only the first member can be initialized. |
This complete program demonstrates how assigning a value to one member corrupts/overwrites earlier members:
#include <stdio.h>
#include <string.h>
/* Define union with 3 different data types */
union DataRecord
{
int i;
float f;
char str[20];
};
int main()
{
union DataRecord record;
printf("Size of union DataRecord = %zu bytes\n\n", sizeof(record));
/* 1. Storing integer */
record.i = 101;
printf("record.i = %d\n", record.i);
/* 2. Storing float - overwrites record.i */
record.f = 220.5f;
printf("record.f = %.2f\n", record.f);
/* 3. Storing string - overwrites record.f and record.i */
strcpy(record.str, "C Programming");
printf("record.str = %s\n\n", record.str);
/* Demonstrating corruption when reading all at once */
printf("--- Reading all members after writing string ---\n");
printf("record.i : %d (Corrupted garbage)\n", record.i);
printf("record.f : %f (Corrupted garbage)\n", record.f);
printf("record.str : %s (Valid current value)\n", record.str);
return 0;
}
Test your understanding of unions and memory sharing.
Part 1: Spot the Bug & Predict the Output
Problem 1: What is the output of sizeof(union Test)?
union Test {
char a;
int b;
double c;
};
Answer & Explanation: Output is 8 bytes (on typical systems). The members are char (1 byte), int (4 bytes), and double (8 bytes). A union allocates memory based on its largest member (double = 8 bytes).
Part 2: Multiple Choice Questions (BCA Exam Preparation)
Q1. Which member of a union can be initialized at the time of declaration?
A) Any member
B) Only the largest member
C) Only the first declared member
D) All members simultaneously
Correct Answer: C) Only the first declared member.
Q2. What operator is used to access union members via a union pointer?
A) Dot operator (.)
B) Arrow operator (->)
C) Asterisk (*)
D) Ampersand (&)
Correct Answer: B) Arrow operator (->).
Part 3: Frequently Asked University Viva Questions
1. When should a programmer choose a union over a structure?
Answer: When you have an entity that can hold only one of several possible data types at any given time (e.g., a variant record, sensor readings that report either an integer code or a float measurement), using a union saves substantial memory.
2. Can a union contain a structure as a member, or vice versa?
Answer: Yes. C supports nested structures and unions (structures inside unions, or unions inside structures), which is common in low-level operating system headers and network packet parsers.