Unions in C Tutorials
Programming
Unions in C
A union is a user-defined data type in C that allows different data members to share the same memory location.
A union is similar to a structure, but there is one major difference:
In a structure, each member gets its own memory location.
In a union, all members share the same memory location.
Because all members use the same memory, only one member can hold a valid value at a time.
Why Use Unions?
Unions are mainly used to save memory.
When different types of data are needed at different times, a union allows them to share the same memory space instead of allocating separate memory for each member.
Example of Union
#include <stdio.h>
union Student
{
int rollNo;
float height;
char firstLetter;
};
int main()
{
union Student data;
data.rollNo = 21;
printf("%d\n", data.rollNo);
data.height = 5.2;
printf("%.2f\n", data.height);
data.firstLetter = 'N';
printf("%c", data.firstLetter);
return 0;
}Output
text
21
5.20
NHow Union Works
Consider the following union:
union Student
{
int rollNo;
float height;
char firstLetter;
};All members share the same memory location.
When we execute:
data.rollNo = 21;the memory contains the value 21.
Later:
data.height = 5.2;overwrites the previous value.
Again:
data.firstLetter = 'N';overwrites the memory.
Only the most recently assigned member contains a meaningful value.
Union Declaration
A union is declared using the union keyword.
Syntax
union UnionName
{
data_type member1;
data_type member2;
data_type member3;
};Example
union Employee
{
int id;
float salary;
char grade;
};Creating Union Variables
After declaring a union, we can create union variables.
Example
union Employee emp;Now emp can access all union members.
Accessing Union Members
Union members are accessed using the dot (.) operator.
Example
emp.id = 101;
printf("%d", emp.id);Difference Between Structure and Union
Structure
Each member gets separate memory.
Multiple members can store values simultaneously.
Size is the sum of all member sizes (plus padding).
Example:
struct Student
{
int rollNo;
float marks;
};Union
All members share the same memory.
Only one member can store a valid value at a time.
Size equals the size of the largest member.
Example:
union Student
{
int rollNo;
float marks;
};Size of Union
The size of a union is always equal to the size of its largest member.
Example
#include <stdio.h>
union A
{
int x;
char y;
};
union B
{
int arr[10];
char y;
};
int main()
{
printf("Sizeof A: %zu\n", sizeof(union A));
printf("Sizeof B: %zu\n", sizeof(union B));
return 0;
}Output
text
Sizeof A: 4
Sizeof B: 40Explanation
Union A
union A
{
int x;
char y;
};int = 4 bytes
char = 1 byte
Largest member = int
Size of union = 4 bytes
Union B
union B
{
int arr[10];
char y;
};int array = 40 bytes
char = 1 byte
Largest member = int array
Size of union = 40 bytes
Nested Union
A union can contain another union as its member.
This is called a nested union.
Example
#include <stdio.h>
union Student
{
int rollNo;
union Academic
{
int marks;
} performance;
};
int main()
{
union Student stu;
stu.rollNo = 21;
printf("%d\n", stu.rollNo);
stu.performance.marks = 91;
printf("%d", stu.performance.marks);
return 0;
}Output
text
21
91Anonymous Union
An anonymous union is a union without a name.
Its members can be accessed directly within the containing structure.
Example
#include <stdio.h>
struct Student
{
int rollNo;
union
{
int marks;
char grade;
};
};
int main()
{
struct Student stu;
stu.rollNo = 21;
stu.marks = 95;
printf("%d\n", stu.rollNo);
printf("%d", stu.marks);
return 0;
}Output
text
21
95Memory Representation of Union
Consider:
union Data
{
int num;
float value;
char ch;
};All members share the same memory block.
text
Memory Block
-----------------
| num |
| value |
| ch |
-----------------Only one value is stored at a time.
Changing one member affects all others.
Applications of Unions
Memory Optimization
Unions help reduce memory usage when only one value is required at a time.
Example
A program may need either:
Student Roll Number
Student Grade
but never both simultaneously.
A union can save memory in such cases.
Hardware Programming
Unions are commonly used in embedded systems and device drivers.
A hardware register can be interpreted in multiple ways using unions.
Variant Data Storage
Sometimes data can be one of several types.
Examples:
Integer
Float
Character
A union allows all these possibilities while using a single memory block.
Communication Protocols
Unions are used in networking and communication systems to represent different packet formats efficiently.
Advantages of Unions
Memory efficient.
Useful for low-level programming.
Supports multiple data interpretations.
Commonly used in embedded systems.
Reduces overall memory consumption.
Disadvantages of Unions
Only one member contains a valid value at a time.
Writing to one member overwrites others.
Can make programs harder to understand.
Improper use may lead to unexpected results.
Structure vs Union Example
#include <stdio.h>
struct DemoStruct
{
int x;
float y;
};
union DemoUnion
{
int x;
float y;
};
int main()
{
printf("Structure Size = %zu\n",
sizeof(struct DemoStruct));
printf("Union Size = %zu",
sizeof(union DemoUnion));
return 0;
}Possible Output
text
Structure Size = 8
Union Size = 4This demonstrates the memory-saving capability of unions.
Summary
A union is a user-defined data type that allows multiple members to share the same memory location.
Only one member can store a valid value at a time.
The size of a union is equal to the size of its largest member.
Union members are accessed using the dot (
.) operator.Unions support nested and anonymous unions.
They are widely used for memory optimization and low-level system programming.
Unlike structures, union members do not have separate memory allocations.