Union in C Programming Language

Union in C Programming


A union is a special data type available in C that allows storing different data types in the same memory location. You can define a union with many members, but only one member can contain a value at any given time. Unions provide an efficient way of using the same memory location for multiple-purpose.

Unions are quite similar to structures in C. Like structures, unions are also derived types.

union car
{
  char name[50];
  int price;
};

Defining a union is as easy as replacing the keyword struct with the keyword union.

How to create union variables?

Union variables can be created in similar manner as structure variables.

union car
{
  char name[50];
  int price;
} car1, car2, *car3;

OR

union car
{
  char name[50];
  int price;
};

int main()
{
  union car car1, car2, *car3;
  return 0;
}

In both cases, union variables car1car2 and union  pointer  variable car3 of type union car is created.

Accessing members of a union

Again, the member of unions can be accessed in a similar manner as structures.

In the above example, suppose you want to access price for union variable car1, it can be accessed as:

car1.price

Likewise, if you want to access price for the union pointer variable car3, it can be accessed as:

(*car3).price
or;
car3->price

More memory is allocated to structures than union

As seen in the above example, there is a difference in memory allocation between union and structure.

The amount of memory required to store a structure variable is the sum of memory size of all members.

Memory allocation of structure variable in C programming

But, the memory required to store a union variable is the memory required for the largest element of an union.

Memory allocation of union variable in C programming

Program

 #include
#include
 
union student 
{
            char name[20];
            char subject[20];
            float percentage;
};
 
int main() 
{
    union student record1;
    union student record2;
 
    // assigning values to record1 union variable
       strcpy(record1.name, "Raju");
       strcpy(record1.subject, "Maths");
       record1.percentage = 86.50;
 
       printf("Union record1 values example\n");
       printf(" Name       : %s \n", record1.name);
       printf(" Subject    : %s \n", record1.subject);
       printf(" Percentage : %f \n\n", record1.percentage);
 
    // assigning values to record2 union variable
       printf("Union record2 values example\n");
       strcpy(record2.name, "Mani");
       printf(" Name       : %s \n", record2.name);
 
       strcpy(record2.subject, "Physics");
       printf(" Subject    : %s \n", record2.subject);
 
       record2.percentage = 99.50;
       printf(" Percentage : %f \n", record2.percentage);
       return 0;
}
 

Output

 Union record1 values example
 Name       :
 Subject    :
 Percentage : 86.500000

Union record2 values example
 Name       : Mani
 Subject    : Physics
 Percentage : 99.500000
Press any key to continue . . .