unions in c are special type of composite data structures that allows different data types to be stored in the same memory location.
Unions in C are similar to structures but with a significant distinction. When a structure is defined, it reserves memory for each individual member. The total memory for the structure is the sum of memory allocated for all its members. On the other hand, a union reserves memory equal to the size of its largest member. This means a union uses only enough memory to hold its largest member, allowing different types of data to share the same memory space. However, because only one member can be accessed at a time, unions are often used in specific cases where you need to optimize memory usage.
union UnionName {
data_type member1;
data_type member2;
// ... (more members if needed)
};
A union can hold only one of its members at a time, but you can change which member is stored. Accessing any member of the union gives you the value based on the data type of that member.
Structures vs Unions in c
lets diffenrentiate unions and structures in simple terms,
structures:
Imagine a structure like a multi-compartment box.
Each compartment (member) holds something different.
You can access and use all the compartments at the same time.
Unions:
Think of a union like a single-compartment box.
The box can hold only one thing at a time.
If you put something new in, it replaces what was there before.
here is a sample program on how to use unions:
#include <stdio.h>
// Define a union with int, char, and float members
union Data {
int intValue;
char charValue;
float floatValue;
};
int main() {
union Data data; // Declare a variable of the union type
// Assign a value to the int member of the union
data.intValue = 65; // ASCII value of 'A'
printf("Character: %c\n", data.charValue);
// Assign a value to the char member of the union
data.charValue = 'X'; // Assign a character
printf("Integer: %d\n", data.intValue);
// Assign a value to the float member of the union
data.floatValue = 3.14; // Assign a float value
printf("Float: %f\n", data.floatValue);
return 0;
}
u can see that each member is assigned and accessed separately, this is because of shared memory. assigning members at the same time leads to data corruption
lets have a real time example for better understanding of union:
Not all the colleges follow the same way of results, lets take three different types of results
- grades :- O(outstanding 90+),A (80+), B (70+), C(60+), F(fail)
- cgpa :- grade points like 9.7, 9.0, 7.8 etc
- percentages :- 90%, 80%, 78% etc.
In this case student can have any one of the above three cases and all three are different types of data. To be an efficient program the best way is to use unions, lets see how:
#include <stdio.h>
union result {
char grade;
float cgpa;
int percent;
};
struct student {
char name[50];
int grade_type; // to know the type of grade used
union result res;
};
int main()
{
struct student stu;
printf("Enter student name:");
scanf("%[^\n]s", stu.name);
printf("enter grade type, 0:grade, 1:cgpa, 2:percentage\n");
scanf("%d", &stu.grade_type);
printf("enter result:\n");
/* get the grade based on given type of grade */
switch (stu.grade_type) {
case 0:
scanf("%c", &stu.res.grade);
break;
case 1:
scanf("%f", &stu.res.cgpa);
break;
case 2:
scanf("%d", &stu.res.percent);
break;
}
/* print and check the output */
printf("Name: %s\n", stu.name);
switch (stu.grade_type) {
case 0:
printf("result:%c\n", stu.res.grade);
break;
case 1:
printf("result:%.1f\n", stu.res.cgpa);
break;
case 2:
printf("result:%d\n", stu.res.percent);
break;
}
}
output:
Enter student name: Mark Zuck
enter grade type, 0:grade, 1:cgpa, 2:percentage
1
enter result:
9.8
Name: Mark Zuck
result:9.8