C Programming Tutorial
Like structure, Union in c language is a user defined datatype that is used to hold different type of elements.
But it doesn't occupy sum of all members size. It occupies the memory of largest member only. It shares memory of largest member.
Advantage of union over structure
It occupies less memory because it occupies the memory of largest member only.
Disadvantage of union over structure
It can store data in one member only.
Defining union
The union keyword is used to define union. Let's see the syntax to define union in c.
union union_name
{
data_type member1;
data_type member2;
.
.
data_type memeberN;
};
Let's see the example to define union for employee in c.
union employee
{ int id;
char name[50];
float salary;
};
C Union example
Let's see a simple example of union in C language.
#include <stdio.h>
#include <string.h>
union employee
{ int id;
char name[50];
}e1; //declaring e1 variable for union
int main( )
{
//store first employee information
e1.id=101;
strcpy(e1.name, "Rama");//copying string into char array
//printing first employee information
printf( "employee 1 id : %d\n", e1.id);
printf( "employee 1 name : %s\n", e1.name);
return 0;
}
Output:
employee 1 id : 1634558290
employee 1 name : Rama
As you can see, id gets garbage value because name has large memory size. So only name will have actual value.
How to create union variables?
Union variables can be created in similar manner as structure variables.
union car
{
char name[50];
int price;
} car1, car2, *car3;
OR
union car
{
char name[50];
int price;
};
int main()
{
union car car1, car2, *car3;
return 0;
}
In both cases, union variables car1, car2 and union pointer variable car3 of type union car is created.
Accessing members of a union
Again, the member of unions can be accessed in similar manner as structures.
In the above example, suppose you want to access price for union variable car1, it can be accessed as
car1.price
Likewise, if you want to access price for the union pointer variable car3, it can be accessed as:
(*car3).price
or;
car3->price
Difference between union and structure
Though unions are similar to structure in so many ways, the difference between them is crucial to understand.
The primary difference can be demonstrated by this example:
#include <stdio.h>
union unionJob
{
//defining a union
char name[32];
float salary;
int workerNo;
} uJob;
struct structJob
{
char name[32];
float salary;
int workerNo;
} sJob;
int main()
{
printf("size of union = %d", sizeof(uJob));
printf("\nsize of structure = %d", sizeof(sJob));
return 0;
}
Output
size of union = 32
size of structure = 40
More memory is allocated to structures than union
As seen in the above example, there is a difference in memory allocation between union and structure.
The amount of memory required to store a structure variable is the sum of memory size of all members.
But, the memory required to store a union variable is the memory required for the largest element of an union.
Only one union member can be accessed at a time
In the case of structure, all of its members can be accessed at any time.
But, in the case of union, only one of its members can be accessed at a time and all other members will contain garbage values.
#include <stdio.h>
union job
{
char name[32];
float salary;
int workerNo;
} job1;
int main()
{
printf("Enter name:\n");
scanf("%s", &job1.name);
printf("Enter salary: \n");
scanf("%f", &job1.salary);
printf("Displaying\nName :%s\n", job1.name);
printf("Salary: %.1f", job1.salary);
return 0;
}
Output
Enter name
Rama
Enter salary
25000.50
Displaying
Name: Rama
Salary: 25000.50
Note: You may get different garbage value or empty string for the name.
Initially in the program, Hillary is stored in job1.name and all other members of job1, i.e. salary, workerNo, will contain garbage values.
But, when user enters the value of salary, 25000.50 will be stored in job1.salary and other members, i.e. name, workerNo, will now contain garbage values.
Thus in the output, salary is printed accurately but, name displays some random string.
Passing Union To a Function
Union can be passed in similar manner as structures in C programming.