Union in C Programming

Union in C programming language is similar to structures. Union is a user defined data structure which is used to store different data type in same memory location. You can define a union with many members, but at any given instance of time only one member can contain value.

For union variable, the storage space allocated is equal to the storage space needed by the largest data member of the union.

Declaration of Union in C

Keyword union is used to define a union. The syntax to define union is similar to defining a structure in C. The union declaration defines a custom data type, that can store multiple member variables of different data types in same memory location.

union union_name
{
data_type variable_name1;
data_type variable_name2;
.
.
data_type variable_nameN;
};

For Example

union Employee {
   char name[32];
   int age;
   float salary;
};

A variable of type Employee union can store either a string(name of employee) value , an integer(age of employee)value or a float(salary of employee) value at at time but not more than one member. A union variable will only get enough memory to store the largest member of union.

For a variable of union Employee will occupy 32 bytes of memory to store the largest member which is name of employee.

C Program to show the Memory Allocated to a Union Variable

Union in C Programming

#include <stdio.h>
#include <conio.h>
  
union Employee {
   char name[32];
   int age;
   float salary;
};
  
int main(){
   union Employee employee;        
 
   printf("Memory occupied by Employee variable : %d\n", sizeof(employee));
 
   getch();
   return 0;
}

Output

Memory occupied by Employee variable : 32

Advantage of Union over Structure
Unions efficiently utilize the same memory location for multi-purpose. It occupies less memory because the storage space needed by the union is size of the largest data member.

Disadvantage of Union over Structure
Unions cannot store multiple value at a time.

Difference between Structure and Union

  • In Structure, all the data members are stored in separate memory locations whereas in Union all members share same memory location.
  • The memory occupied by a structure variable is equal to sum of the memory of all structure members whereas the memory occupied by a union variable is equal to the storage space needed by the largest data member of the union.
  • At a time, a union variable can contain only one active member, whereas a structure variable contains all member.

C Program to show the use of Union

C Program to show the use of Union

#include <stdio.h>
#include <string.h>
#include <conio.h>
  
union Employee {
   char name[32];
   int age;
   float salary;
};
  
int main(){
   union Employee employee;
   /* Using one member of a union at a time */
   strcpy(employee.name, "Jack");
   printf("Name = %s  Address = %p\n", employee.name,
       &employee.name);
    
   employee.age = 30;
   printf("Age = %d Address = %p\n", employee.age, 
       &employee.age);
    
   employee.salary = 1234.5;
   printf("Salary = %f Address = %p\n", employee.salary,
       &employee.salary);
    
   /* Printing all member variable of Union, Only last updated 
      member will hold it's value remaining will contain garbage */
   printf("\nName = %s\n", employee.name);
   printf("Age = %d\n", employee.age);
   printf("Salary = %f\n", employee.salary); 
    
   getch();
   return 0;
}

Output

Name = Jack Address = 0028FF20
Age = 30 Address = 0028FF20
Salary = 1234.500000 Address = 0028FF20

Name = 
Age = 1150963712
Salary = 1234.500000

Above program shows that we are storing different member variables of a union in same memory location. However a union variable can only retain the value of most recently updated member variable.