Like Structure Union is also an important concept in C. There are two types that data type exists in C. They are primitive data types and non-primitive or User-defined data types. Union is an example of a non-primitive data type.
What are the Union and its Main Features
Union is a way to group variables. Sometimes we have to store some information in a group.
Suppose we have to store some students’ information. Information means their name, address, mobile no, gender, marks, aggregates, etc. A situation occurs when only one data member of this group of variables has to contain a value at a time. In this situation, if we store these data to the multiple variables for each student, the program becomes very complex and increases the program’s execution speed.
To reduce this problem, we introduce Union. With the help of the union, we can group these variables of each student in a single variable and contain one of these groups of variables at a time.
Union is a collection of dissimilar elements. Dissimilar means we can store different data type values like int, char, float, string, etc.
If we store a student’s information, we have to store his or her name, marks, mobile no, etc. Name is a string, mark is a float, mobile no is an integer array. So it is a group of dissimilar elements.
Defining Union means creating a new data type, and we know that creating a new data type means we use here non-primitive data type.
Union is a user-defined data type like Structure. But union members share the same memory location.
Example
Union example
int x ;
char y ;
} e ;
If we print the address of x and y., they can contain the same address location
There can exist different types of data members in the union. But union’s size is calculated according to the largest data member present in the union.
Programming Example 1
union item // defining another union ;
{
int x ;
float y ;
char z ;
};
int main ()
{
printf ( “ %d ”, sizeof ( union item )) ;
return 0 ;
}
Output
Explanation
Here we define a union called item. Data members of this union are x, y, and z, which are integer, float, and character type data, respectively. Now inside the main () function, we use the sizeof operator to determine the size of the data type item. It gets output 4 as the float data type is the highest data type present in the union as a data member.
How to Define a Union
{
data type var1 ;
data type var2 ;
-------------------- ;
data type varn ;
};
union item // defining another union ;
{
int x ;
float y ;
char z ;
};
Memory Consumption
No memory is consumed for the definition of Union. Because memory is consumed at the time of declaration of a variable but not in the case of defining data type.
As defining Union means creating a new data type, memory is not consumed. Memory is consumed at the time of declaration of a particular type of variable.
Structure vs. Union
Struct item union item
{ {
int x ; int x ;
float y ; float y ;
char z ; char z ;
} }
Struct item i1 ; union item i1 ;
Explanation
In Structure, we can create the memory location for a group of variables. All the data member of the structure contains memory at a time.
Where in a union, we can create the memory block of the highest memory contains that exists as a member variable.
In structure we create a structure which takes (2 + 4 + 1) = 7 bytes. As integer, float, char takes 2, 4, and 1-byte memory, respectively.
Where in the union, we create a union which takes 4 bytes memory block as float data type variable exist here as the highest data type member variable.
Advantages of Union
With the help of the union, we can access any data once at a time. If we want to access another member variable, it means that the variable or value of the variable overwrites the previous value of a variable. It means it takes the value of the latest variable which exists in the union.
Programming Example 2
Output
Explanation
From the output, we can have the wrong perception that union consumes memory space ( 2 + 4 + 1 ) = 7 bytes. But it is a wrong perception. Because when we use integer variable, we can use this. Then we use the float variable; that variable overwrites the value of x.
After using the value of y, if we want to print the value of x again, it gets output x = 0. It can consume the memory of one variable at a time of the highest data type variable.
Programming Example 3
Output
Explanation
Here we define a union named example. Inside the union, there are two data members who exist. One is integer type variable x; another one is character type variable z. Inside the main () function, x is assigned 65.
Now, if we print the value of x, then it shows a value of 65. Surprisingly if we print the value of y, it shows result A. As the data members of the union share the same address location of our memory and the binary value of 65 is A, it shows the result A as a character value.
Conclusion
Like Structure Union is also an important concept in C. Through the union, we learn that many data members or variables can share the same address location in our memory. So it is a unique feature of the union.