Union Program in C
How should a union be defined?
In C programming, a Union is a method used to organize different data types into groups in the same way that Structures are used. We are able to make user-defined data type elements by utilizing a union.
In contrast to Structures, a C union variable will allot shared memory to all of its members (i.e., book cost, book name, Writer’s name, etc.). The allocated shared memory has a size that is equivalent to that of the biggest union member. We can save a lot of memory (RAM) because of this.
We are unable to access every union member simultaneously due to this shared memory. The C union variable can only contain one member at a time. Therefore, we can only access one member at a time.
The union is also a grouping of many items with varying properties stored in close proximity to one another in memory. They are custom data types. The structure's components are referred to as the union's members, and the union name (like date, book, etc.) is handled as a data type.
When a union is defined, no memory is allocated. Only after the creation of its variables, memory is allocated (It is typically preceded by the keyword union). Among all of its members, the variables of union types take up the most memory space. Only one of its members may store data at once and concurrently. Using the dot (.) operator, you may access the members.
Difference between Structure and Union
- In Structure, all the data members are stored in separate memory locations, whereas in Union, all members share the same memory location.
- A union variable's memory usage is equal to the amount of storage space required by the largest data member of the union, as opposed to a structure variable's memory usage, which equals the total of all the memory of the structure's members.
- Unlike a structure variable, which contains all members, a union variable can only have one active member at a time.
Union Syntax in C:
union box
{
int x;
char y;
float z;
};
union box b1;
In this syntax, we initialized a union named box, which has three data members(int, char, float). Here union is the keyword, and the box is the data type.
Among all these members, the member who is taking the most memory, the box will get the same amount of memory. This indicates that
- int takes 2-byte memory.
- Char takes 1-byte memory.
- Float takes 4-byte memory.
So maximum memory which is allotted to the box will be 4 bytes.
Here b1 is the variable of box data type
x,y, and z are the member variables.
1. Example Program for C Union:
#include<stdio.h>
union box
{
int x;
char y;
float z;
};
void main()
{
union box b1;
b1.x =3;
printf("\n x=%d",b1.x);
b1.y='c';
printf("\n y=%c",b1.y);
b1.z=3.8;
printf("\n z=%f",b1.z);
}
Explanation:
- Union is the keyword, and the box is the data type.
- b1 variable has three member variables x,y, and z.
- The dot(.) operator is used to access member variables.
b1.x =3; b1.y='c'; b1.z=3.8;
- For int data type, we use %d format specifier.
- For char data type, we use %c format specifier.
- For float data type, we use %f format specifier.
Program Output:

2-Example for C union Program:
#include <stdio.h>
#include <string.h>
union student
{
char name[15];
int age;
float percentage;
};
int main()
{
union student data;
printf("Student data is :\n");
strcpy(data.name, "Durgesh");
printf("Name : %s \n", data.name);
data.age=18;
printf("Age: %d \n", data.age);
data.percentage = 87.63;
printf("Percentage : %f \n", data.percentage);
return 0;
}
Explanation :
- “Durgesh” is assigned to union member “data.name”. The memory location name is “data.name” and the value stored in this location is “Durgesh”.
- Then, “18” is assigned to union member “data.age”. Now, the memory location name is changed to “data.age” with the value “18”.
- Then, “87.63” is assigned to union member “data.percentage”. Now, the memory location name is changed to “data.percentage” with the value “87.63”.
- In a similar manner, the name and value of each union member are periodically changed in the communal storage area.
- As soon as values are assigned to each union member, they may be retrieved.
- Because all members share the same memory, if we don't access them before giving values to other members, the other members will overwrite the member's name and value.
- All union members cannot be accessed at once.
Program Output:

Conclusion: So, we can say that,the union makes memory management easier. Only the most recent value submitted into the union will be available, which is the union's disadvantage. When union members don't need to be accessible at the same time, this method should be utilized.