What is Union in C Programming?

Welcome guys, in this module we are going to discuss what is union in C Programming, its characteristics, its uses, its advantages and disadvantages, the difference between the structure and union, etc.
So, let’s begin with this module.

What is Union in C Programming?

To store data of different data types in the same location, we have a union in C for this particular purpose. It is a user-defined data type, just similar to the structure in C, the only key difference is that the union allows storing of only the largest data members at a time whereas the structure allows storing all of his data members.

What is Union in C Programming

It is known for reusing the memory location in the best and efficient way as it accesses only one member at a time. It can be defined with many data members but only one data member can contain a value at a time.

How to define a union in C Programming?

The union can be defined in the same way as we declare the structure in C. Let sees one example to understand.

union Person {
char name [20];
int age;
};

We have to use the union keyword to define union in C. In this example, we have defined a derived type union Person and given the two data members in the same.

How to create a Union variable?

Now since we have defined the union, and in order to access the member of the union, we have to make a union variable. When the union is declared no memory is allocated at the starting until or unless the union variable is declared.

Here is an example demonstrating the union variable.

union Person {
char name [20];
int age;
};
int main ( )
{
  union Person Person1, Person2;
  return 0;
}

Or the same can be defined as.

union Person {
char name [20];
int age;
} Person1, Person2;

Above are the two ways, in which the union variables can be defined, here the union variables are Person1 and Person2.

How to access the members of the Union?

Accessing the members means using the data members of the union in our program and that we can achieve this by using a dot ( . ) operator. Let us understand it by taking an example.

#include <stdio.h>
// union Person defined
union Person {              
int age;
int salary;
} ;
int main()
{   // union variable declared 
    union Person Person1;     
    // accessed the union member and assigned the value  
    Person1.age = 23;        
    // printing the value       
    printf("The age is: %d \n",Person1.age);    
    // accessed the another union member and assigned the value  
    Person1.salary = 40000;      
    // printing the value 
    printf("The salary is: %d",Person1.salary);     
    return 0;
}

The output of the above program is.

The age is: 23
The salary is: 40000

In the above program, we have defined the union Person with two data members and then assigned the value to that data member using the dot operator and union variable, then printing the same value as output.

What are the differences between structure and union in C?

Since we know that there is a lot of similarity between structure and union, so let’s see some of the main differences between them both.

Structure Union
While defining a structure, a struct keyword is used. While defining a union, a union keyword is used.
Modifying any value of the members of the structure, will not affect any other values of the structure. Modifying any value of the members of the union will affect other values of the union.
Any member can be accessed anytime in the program. Only one member can be accessed at a time, you can access two or more members at a time.
Each and every single member is assigned to a unique space in the memory. In this, the allocation of the memory is shared by the individual members of a union.
#include <stdio.h>
// union Person defined
union Person {     
char name[20];
int age;
int salary;
};
// structure Person_new defined
struct Person_new {       
char name[20];
int age;
int salary;
} ;
int main()
{   // Union variable declared
    union Person Person1;
    // structure variable declared
    struct Person_new per; 
    // computing the memory occupied by Union               
    printf("The size of union is: %d\n", sizeof(Person1));
    // computing the memory occupied by Structure
    printf("The size of structure is: %d", sizeof(per));  
    return 0;
}

The output of the above program is.

The size of union is: 20
The size of the structure is: 28

In the above program, we have defined union Person and the structure Person_new and have declared 3 data members in them, then printed the memory allocated by the union and structure both, so we can see that the union size is less than the structure size because the union has taken the space equal to the size of the larger data types of his member whereas structure has allocated the space for all his data members So, this program clearly depicts the memory allocation technique.

What are the advantages of using a union?

Some of the best reasons to use union is.

  1. It allocates the space in the memory of the data members who have the highest memory, i.e., in this case, the memory consumption is less.
  2. Through this, you can hold data of only one data member.
  3. The last variable can be accessed directly in the union.
  4. When you have to use the same location for one or more data members, then unions are used.

What are the disadvantages of using a union?

Let’s see some of the disadvantages of the union.

  1. Only one member can be accessed at a particular period of time.
  2. Only one storage space is assigned to all the variables.
  3. All the variables cannot be assigned with varying values at a time.

I hope, this module helped you all a lot in gaining knowledge about all the concepts included in the what is union in C Programming and must be excited to implement the same. Stay connected for such more modules, until then keep learning, happy coding!