Union in C Language – Unveil the Difference between Structures and Unions

Get Certified in C Programming for Free and Take Your Skills to the Next Level

Just like structures, union in C is a user-defined data type used to store data of different types that hold the same memory location.

In this tutorial, we will discuss the following-

  • Union in C
  • Difference between Structures and Unions in C
  • Meaning of Pointers to a Union in C

Before we begin our discussion on unions, it is important to have a crystal clear understanding of structures in C.

Union in C Language

1. Union in C Language

As the name itself suggests, a union refers to the grouping together of data members treated as a single entity. The keyword union is used to indicate the declaration of a union. The format to define a union is the same as that of structures.

A union can be defined in 2 ways, just like structures. They are:

1.1 When Union variables are created inside the main function

union union_name
{
data_type member1;
data_type member2;
data_type member3;
.
.
.
data_type membern;

};
int main()
{
union v1, v2, *v3; // Here v1, v2 and *v3 are variables to the union
/* Body of the main function */
return 0;
}

1.2 When Union variables are created outside the main function

union union_name
{
data_type member1;
data_type member2;
data_type member3;
.
.
.
data_type membern;

}v1, v2, *v3; // Here v1, v2 and *v3 are variables to the union
int main()
{
/* Body of the main function */
return 0;
}

Key takeaway: In order to access a data member through pointer *v3, the -> operator is used.

2. How to Implement Unions in C Programming?

Here is a code in C that illustrates the implementation of unions in a C program:

#include <stdio.h>
union DataFlair
{
char value1, value2;
};
int main()
{
printf("Welcome to DataFlair tutorials!\n\n");
union DataFlair d; // Here d is a variable of union DataFlair
d.value1 = 'a'; // Here d.value2 will automatically become 'a'
printf("value1 = %c, value2 = %c\n",d.value1, d.value2);
d.value2 = 'b'; // Here d.value1 will automatically become 'b'
printf("value1 = %c, value2 = %c\n",d.value1, d.value2);
return 0;
}

Code on Screen-

 implementation of unions in a C

Output-

Output of Implementation of Unions in C Language

3. What is the Use of Union in C Programming?

Here is another code in C that illustrates the use of unions in a C program

#include <stdio.h>
union employee
{
int ID;
int age;
};
int main()
{

printf("Welcome to DataFlair tutorials!\n\n");

union employee e;
e.ID = 1001;
e.age = 22;

printf("The ID is: %d\n",e.ID);
printf("The age is: %d\n",e.age);
return 0;
}

Evidently, it is clear that only one member can be accessed at a time in a union.

Code on Screen-

Use of Unions in C 

Output-

Usage of Unions in C

4. Structures Vs Unions in C

There are a lot of similarities between structures and unions in C programming language, such as they have similar syntax and serve the same purpose used to store data members of different data types.

But, there are a couple of differences between the two. One of the striking differences between the two is that the data members in a structure have individual memory locations, whereas the data members of a union share the same memory location that gives rise to its distinction.

structure and union in C

Distinctive Feature
Structure in CUnion in C
KeywordIt is pretty obvious that a structure begins with a struct keyword.
A union begins with a union keyword.
Memory location
All the data members of a structure have their own individual memory location for their storage. Therefore, if changes are made in one data member, the other members would be independent of it.
All the data members of a union share the same memory location. Hence, changes made in one data member inevitably affects the other.
InitializationIt is possible to initialize various data members of a structure through an object at one time.
A union prohibits the initialization of all its data members. The first member of the union can only be initialized through an object.
SizeThe total size of a structure is the sum of the individual sizes of its data members.
The total size of a union is the largest size of its data members.
AccessWe can access all the data members of the structure at one time.
Only one data member of a union can be accessed at one time.

Here is a code in C that illustrates the difference between a structure and a union:

#include <stdio.h>
struct sample_structure
{
char name[30]; // size = 30
int emp_id; // size = 4
float salary; // size = 4
}s;
/* Total size of union = Largest size of data member = 30 */
union sample_union
{
char name[30]; // size = 30
int emp_id; // size = 4
float salary; // size = 4
/* Total size of structure = 30 + 4 + 4 = 38 */
}u;

int main()
{

printf("Welcome to DataFlair tutorials!\n\n");

printf("The size of the structure is : %ld bytes\n", sizeof(s));
printf("The size of the union is : %ld bytes\n", sizeof(u));
return 0;
}

Code on Screen- 

difference between a structure and a union in C

Output-

structure Vs unions in C

5. Pointers to Unions in C

Pointers to unions is a similar concept like pointers to structures in C.

Here is a code in C that illustrates the use of pointers to unions:

#include <stdio.h>
union sample
{
int number;
char character;
};

int main()
{

printf("Welcome to DataFlair tutorials!\n\n");

union sample s; // Here p is a pointer
s.number = 122;
union sample *p = &s;

printf("The ASCII value %d corresponds to %c\n", p->number, p->character); // Displaying the character corresponding to the ASCII value
return 0;
}

Code on Screen-

How to use of pointers to unions

Output-

Use of pointers to unions

Quiz on Union in C

6. Summary

In this tutorial, we discussed what are unions, how to define them and various C programs associated with it. We saw the contrasting differences between a union and structure, although they seem to be quite similar to each other. Hope, you liked Union in C tutorial, but this is not the end, you may also like to know about Macros in C.

Any queries or feedbacks? Enter in the comment section. We will be glad to hear from you.

Did you know we work 24x7 to provide you best tutorials
Please encourage us - write a review on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *