Like Structure in C, Union is also a user-defined data type that is used to store the collection of different data types which are grouped together.
Union and structure both are the same, except allocating memory for their members. Structure allocates storage space for each member separately whereas, Union allocates one common storage space for all its members providing an efficient way of using the same memory location for multi-purpose.
Defining Union
The keyword union
is used to define union. The syntax of union is stated below.
1 2 3 4 5 6 7 8 | union tag_name { data_type var_name1; data_type var_name2; data_type var_name3; ... }; |
Applying above syntax we can write the union for student in following way in C.
1 2 3 4 5 6 | union student { int st_id; char st_name[10]; char st_address[50]; }; |
Creating a Union variable
Union variable is created to allocate the memory then we can perform some task, defining union does not allocate memory. Union variable will always have the size of its largest member
Union variable can be created in two different ways:
1. Union variable can be created inside the main()
function as shown blow.
1 2 3 4 5 6 7 8 9 10 11 12 13 | union student { int st_id; char st_name[10]; char st_address[50]; }; int main() { union student s1, s2, *s3; return 0; } |
2. Union variable can be created after defining the union as shown below.
1 2 3 4 5 6 | union student { int st_id; char st_name[10]; char st_address[50]; } s1, s2, *s3; |
In both s1 and s2 are union variables and s3 is a union pointer variable.
How to access the members of union?
Accessing the (.
) dot operator is used to access the union members and ->
operator is used to access the pointer variables.
Consider the above mention example of union. We can access its members in the following ways:
s1.st_id;
s2._st_name;
(*s3).st_address
ors3->st_address
Example of Union in C programming
We will see two examples, first one will give a garbage value as only one at a time the memory is allocated to one member.
1. Union Program in C: shows garbage value
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #include <stdio.h> #include <string.h> union student { int id; char name[25]; }s1; //declaring union var s1 int main( ) { //storing values s1.id = 101; strcpy(s1.name, "Sherlock Holmes"); //Printing printf( "Student id : %d\n", s1.id); //output: garbage value printf( "Student name : %s\n", s1.name); return 0; } |
Output:
1 2 | Student id : 1919248467 Student name : Sherlock Holmes |
1. Union Program in C: proper result
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #include <stdio.h> #include <string.h> union student { int id; char name[25]; }s1; //declaring union var s1 int main( ) { //storing and printint one at a time s1.id = 101; printf( "Student id : %d\n", s1.id); //output: garbage value strcpy(s1.name, "Sherlock Holmes"); printf( "Student name : %s\n", s1.name); return 0; } |
Output:
1 2 | Student id : 101 Student name : Sherlock Holmes |
Note that in the above example one member is being used at a time that’s why all the members are printed properly else the student id value might get corrupted like the one in first example.
Using one variable at a time is the main purpose of using ‘union’.