Union in C

Like Structures, Union in C Programming is also used to group different data types to organize the data in a structural way. A system reserved keyword union was used to create it in C programming language.

Unlike Structures, the C union variable will allocate the common memory for all of its members (i.e., age, name, address, etc.). The size of the allocated common memory is equal to the size of the largest union member in C. Due to this, we can save a lot of memory.

Due to this common memory, we can’t access all the union members at the same time. Because the C union variable holds one member at a time, we can access one member at a time.

C Union Syntax

The basic syntax of the Union in C Programming is as shown below

union UName
{
  Data_Type Variable_Name; //Member

  Data_Type Variable_Name; //Member

  ………….
};

It is the system reserved keyword used to create and access its members. The UName is the name you want to give for this C union. For example, employees, persons, and students.

Data_Type means the Data type of the variable that you want to declare. For example, int, float, char, etc. And the Variable_Name is the name of the variable. For example, id, age, name, and salary. For Example

union Student //Name = Employee

{

  //Members
  int Roll_No; //Data Type = int; Variable Name = Roll_No

  char name[50]; //Data Type = char Array; Variable Name = name

  char address[100]; //Data Type = Char Array; Variable Name = address

};

Declaration of Union in C Programming

Before accessing the members inside it, we have to create the variables. We can declare the variables in multiple ways. The First Approach is declaring the C union first and then, in the main function, creating the variable.

union Employee

{

  int age;

  char name[50];

  float salary;

};

//Creating the Structure variable

 union Employee emp1, emp2;

 emp1 = {25, "Dave", 25000}

Here, emp1 and emp2 are the variables of this data type. The second approach is to create the C variables at the time of the union declaration.

union Employee

{

  int age;

  char name[50];

  float salary;

} emp1, emp2;

Accessing Members

We can access the members of Union in C Programming using the dot operator (.) or members operator. For example, let us use the above example.

//Assigning the values

 emp1.age = 26;

 strcopy (emp1.name, "Michel");

 emp1.salary = 46000.50;

Union in C Programming Example

In this C program, we are going to declare a union with four members. Then we will display every individual member as output in 2 different ways. Please refer Structures article in C Programming to understand the structures.

#include <stdio.h> 
#include <string.h> 

union Employee 
{
  int age;  
  char Name[50];
  char Department[20];
  float Salary;
};

int main() 
{
  union Employee emp1;
  union Employee emp2;
    
    emp1.age = 28;
    strcpy(emp1.Name, "Chris");
    strcpy(emp1.Department, "Science");
    emp1.Salary = 32000.70;

  printf("\nDetails of the First Employee \n");
 
  printf("  Employee Age = %d \n", emp1.age);
  printf("  Employee Name = %s \n", emp1.Name);
  printf("  Employee Department = %s \n", emp1.Department);
  printf("  Employee Salary = %.2f \n\n", emp1.Salary);

  printf("Details of the Second Employee \n" );

  emp2.age = 30;
  printf("  Employee Age = %d \n", emp2.age );
  
  strcpy(emp2.Name, "David");
  printf("  Employee Name = %s \n", emp2.Name );

  strcpy(emp2.Department, "Technology" );
  printf("  Employee Department = %s \n ", emp2.Department );

  emp2.Salary = 35000.20;
  printf("  Employee Salary = %.2f \n ", emp2.Salary );

  return 0;
}
Union in C programming

Within this C union example program, we declared the Employee with age, Name, Department, and Salary members with appropriate Data Types. Within the main() function, We created 2 variables, emp1, and emp2, for the Employee.

Details of the First Employ.

From the below statement, you can see that the variable will allocate memory for the age member. It means the value of 28 stores in the memory location for age (age = 28).

emp1.age = 28;

From the below statement, you can observe that the C Union variable has to allocate memory for the Name member. So, it will remove the data of the previous entry, i.e., age. Then it will allocate the same memory to the Name member. It means Chris will store it in a memory location (Replaced age here).

strcpy(emp1.Name, "Chris");

Now the variable has to allocate memory for the Department member. So, it will remove the data of the previous entry, i.e., Name. Then it will allocate the same memory to the Department member. It means Science is stored in a memory location (Replaced Chris here).

strcpy(emp1.Department, "Science");

Next, the C Union variable has to allocate memory for the Salary member. So, it will remove the data of the previous entry, i.e., Department, and allocates the same memory to the Salary member. It means 32000.70 stored in the memory location (Replaced Science here).

emp1.Salary = 32000.70;

The below printf statements are used to output the values we assigned for the members. The following statement will display a wrong or strange value because other values have already replaced the age value.

printf(" Employee Age = %d \n", emp1.age);

The following statement will display a wrong or strange value because other values have already replaced the Name value.

printf(" Employee Name = %s \n", emp1.Name);

Below C union statement displays strange or wrong values because the Department value has already been replaced by Salary.

printf(" Employee Department = %s \n", emp1.Department);

The following statement prints the correct Salary value. Because after this, we haven’t assigned the memory location to other values.

printf(" Employee Salary = %.2f \n", emp1.Salary);

Details of the Second Employee. The C Union allows us to access one data member at a time. So in this variable, we will display the output immediately after assigning the value to that data member.

emp2.age = 30;

printf(" Employee Age = %d \n", emp2.age);

The variable will allocate memory for the age member. It means the value of 28 stores in the memory location for age (age = 28). After assigning the value, it is immediately printing the output of age.

Now the C Union variable has to allocate memory for the Name member. So, it removes the data of the previous entry, i.e., age. Then it will allocate the same memory to the Name member. It means David is stored in a memory location (Replaced age here).

strcpy(emp2.Name, "David");

printf(" Employee Name = %s \n", emp2.Name);

TIP: If you forget to print the statement immediately, the data will remove and allocated to the next member.

The process is the same for the below statements also.

strcpy(emp2.Department, "Technology");
printf(" Employee Department = %s \n", emp2.Department);

emp2.Salary = 35000.20;
printf(" Employee Salary = %.2f \n\n", emp2.Salary);