Union in C

The C programming language has many features which may help programmers in easy coding. Unions are defined as a special data type. It is used to store a collection of different types of data. Unions are like structures. With the help of unions, you can use the same memory location for multiple purposes.

What are Unions in C?

Unions are like structures. It is used to store different types of data in the same memory location. The only difference between structures and unions is that unions only hold one value at a time whereas structures hold all their members. With the help of unions, you can define many members but only one of them can contain value at one time.

Defining union in C

As we know that unions can contain many members of different data types but it cannot handle all the members at the same time.

The union keyword is used to define the unions.

Syntax:-

union UnionName{
// member definitions;
}

Example of Union in C

union detail{
int id;
char name[30];
float salary;
};

In the above, we have defined an union type named detail which has three members of different data types such as integer, character and float. Data type will occupy 30 bytes of memory space because character data type occupies 30 bytes of space and this is the maximum space which is occupied.

Creating union variables in C

Union is a user defined data type. To use and allocate memory for the union type, we first need to create variables.

Example of Creating union variables inside main() function

union detail{
int id;
char name[30];
float salary;
};
int main(){
union detail emp1,*emp2;
return 0;
}

In the above example, we created three union type variables. You can also create variables like below:-

Creating union variables outside the main() function

union detail{
int id;
char name[30];
float salary;
}emp1, *emp2;

In both examples, we created union variables such as emp1 and a union pointer to emp2 to union type detail.

Example:- Basic example of union in C

#include <stdio.h>
union detail{
   int bill;
   char name[50];
};
int main( ) {
union detail a;   	 
printf("Total Memory size occupied by detail union type is: %d\n", sizeof(a));
return 0;
}

Output

Total Memory size occupied by detail union type is: 52

Accessing members of union in C

To access the members of union type, we use the . operator. And -> is used to access pointer variables.

Example:- Accessing members of union in C

#include<stdio.h>
#include<string.h>
union bestWebsite
{
   char name[20];
   char tutorial[50];
};
 
void main( )
{
union bestWebsite web;
strcpy( web.name, "TechVidvan Tutorials!");
printf("Best Website to learn C programming is: %s\n",web.name);
strcpy(web.tutorial, "union in C!");  
printf("Now you are doing: %s\n",web.tutorial);
}

Output

Best Website to learn C programming is: TechVidvan Tutorials!
Now you are doing: union in C!

In the above example, we used web.name to access name for variable web and also used web.tutorial to access tutorial for variable web.

Difference between union and structure in C

The main difference between union and structure is that in structure, each variable has its own storage location but in union, all members use the size of its largest data member.

There are a couple of differences between union and structure. Let’s discuss below:-

Distinctive Feature Structure Union
Keyword It begins with the struct keyword. It begins with the union keyword.
Memory location In structure, all members of a structure have their own individual memory location for their storage. In union, all members of a union share the same memory location for their storage.
Initialization You can initialize many data members of a structure at one time. But union forbids the initialization of all its members.
Size Structure size depends on the sum of the individual sizes of its members. In union, total size is the largest size among its data members.
Access All data members of the structure can be accessed at one time.  In union, you can access one data member at one time.

Let’s take a look at the below example:-

#include <stdio.h>
union detail
{
   char s1[32];
   int id;
}employee;

struct custom
{
   char s2[32];
   int id_branch;
}worker;

int main()
{
printf("TechVidvan Tutorials: Difference between union and structure!\n");
printf("Union size is: %d bytes\n", sizeof(employee));
printf("Structure size is: %d bytes", sizeof(worker));
return 0;
}

Output

TechVidvan Tutorials: Difference between union and structure!
Union size is: 32 bytes
Structure size is: 36 bytes

From the above example, we can see that:-

  • The size of custom is 36 bytes because

The size of s2[32] is 32 bytes.
Size of id_branch is 4 bytes.

  • So, Total size of the custom is 36 bytes.

But in union the size of detail is 32 bytes because the size of a union variable will always be the largest of its members.

Pointer to union in C

We can also use pointers to unions like structures. To access the members of unions, you have to use the arrow operator ->.

Example:- pointer to unions

#include <stdio.h>
union point {
  int a;
  double c;
};
int main()
{
  union point point1;
  point1.a = 10;
  union point* point2 = &point1;
  printf("TechVidvan Tutorials: pointers to union!\n");
  printf("%d", point2->a);
  return 0;
}

Output

TechVidvan Tutorials: pointers to union!
10

Use of Union in C

#include <stdio.h>
union student
{
int age;
int total_marks;
};
int main()
{
printf("TechVidvan TUtorial: Use of Unions in C!\n\n");
union student s1;
s1.age = 20;
s1.total_marks = 754;
printf("Age of the student is: %d\n",s1.age);
printf("Total marks of the student is: %d\n",s1.total_marks);
return 0;
}

Output

TechVidvan TUtorial: Use of Unions in C!

Age of the student is: 754
Total marks of the student is: 754

From the above example, it is proved that you can only access one member at one time.

Advantages of Union in C

  • If you want to use same memory locations for two or more members, then you can use
    union.
  • It occupies less memory than structure.

Disadvantages of Union in C

Only big disadvantage is that only last entered data can be stored in union.

Applications of C Union

  • With union, it is so easy to break up a STREAM of input.
  • It is useful in situations where you want to use the same memory locations for two or more members.

Summary

Union is like structure. It is a user-defined data type. It is used to store different types of data in the same memory location.

The main difference between union and structure is that in structure, each variable has its own storage location but in union, all members use the size of its largest data member.

TechVidvan Team

The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today’s tech industry.