Loading content...
Loading content...
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.
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.
#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;
}text
21
5.20
NConsider 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.
A union is declared using the union keyword.
union UnionName
{
data_type member1;
data_type member2;
data_type member3;
};union Employee
{
int id;
float salary;
char grade;
};After declaring a union, we can create union variables.
union Employee emp;Now emp can access all union members.
Union members are accessed using the dot (.) operator.
emp.id = 101;
printf("%d", emp.id);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;
};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;
};The size of a union is always equal to the size of its largest member.
#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;
}text
Sizeof A: 4
Sizeof B: 40union A
{
int x;
char y;
};int = 4 bytes
char = 1 byte
Largest member = int
Size of union = 4 bytes
union B
{
int arr[10];
char y;
};int array = 40 bytes
char = 1 byte
Largest member = int array
Size of union = 40 bytes
A union can contain another union as its member.
This is called a nested union.
#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;
}text
21
91An anonymous union is a union without a name.
Its members can be accessed directly within the containing structure.
#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;
}text
21
95Consider:
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.
Unions help reduce memory usage when only one value is required at a time.
A program may need either:
Student Roll Number
Student Grade
but never both simultaneously.
A union can save memory in such cases.
Unions are commonly used in embedded systems and device drivers.
A hardware register can be interpreted in multiple ways using unions.
Sometimes data can be one of several types.
Examples:
Integer
Float
Character
A union allows all these possibilities while using a single memory block.
Unions are used in networking and communication systems to represent different packet formats efficiently.
Memory efficient.
Useful for low-level programming.
Supports multiple data interpretations.
Commonly used in embedded systems.
Reduces overall memory consumption.
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.
#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;
}text
Structure Size = 8
Union Size = 4This demonstrates the memory-saving capability of unions.
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.