Unions in C Language

Union is a user defined data type, which is used to store different kinds of data. In simple word a union is a memory location that is shared by two or more types of variables.

Union and structure in C  are same in concepts, except allocating memory for their members. In structures, each members has its own storage location, but in Union all the members are use same storage location. The Union allocates one common storage space for all its members, thus it uses less memory space then structure. But with union we can only store information on one union member only at a time.

To understand the difference between union and structure, lets see an example. The below statement declare a union named data.
union data {
  char ch;
  int num;
  float ft;
} set;
The above code create a union variable set of type union data. The union contains three members : ch, num and ft, each with a different data type. However, we can only use one of them at a time. This is due to the fact that only one location is allocated for a union variable, irrespective of its size.

The compiler allocates a piece of storage that is large enough to hold the largest variable type in the union. In the above declaration, the member num (int), and ft (float) both are the largest among the members and requires 4 bytes of space. The below figure shows that how all three variables share the same address.


Defining Union :

Union can be defined by using the keyword union. The declaration of union is same as structure. The syntax is :
union union_name {
  DataType member_name;
  DataType member_name;
  ..................
  DataType member_name;
} union_variable_name;
Where :
  • union_name is name of the union, although it is optional. 
  • member_name is name of the union members.
  • union_variable_name is name of the variable, but it is also optional.
union data {
  char name[16];
  int num;
  float ft;
} dtset;
The above code create a union data with three member variables, with union variable named dtset. The total space take by union data is 16 bytes, because the member variable name[] is the biggest member variables here. We can check that by using sizeof() C function. For example :
#include <stdio.h>

union data {
  char name[16];
  int num;
  float ft;
} dtset;

int main() {
  printf("Size of union data is : %ld\n", sizeof(dtset));
}
Output :

Size of union data is : 16

Declaring Union Variable :

The syntax for declaring union variable is :
 union union_name variable_name;
Example :
 union data dset;
where dset is name of the union variable.

Accessing Union Members :

The union variable can be accessed by using the dot operator ( . ), just like structure. The syntax is :
 union_variable.member_name;
Example :
 dset.name;
 dset.num;
 dset.ft;
Now lets see a C example code :
#include <stdio.h>
#include <string.h>

union data {
  char name[16];
  int num;
  float ft;
};

int main() {
  union data dset;

  strncpy(dset.name, "Ajay Kumar", 16);
  printf("Name : %s\n", dset.name);

  dset.num = 200;
  printf("Number : %d\n", dset.num);

  dset.ft = 45.876;
  printf("Float Number : %.3f\n", dset.ft);

  return 0;
}
Output :

Name : Ajay Kumar
Number : 200
Float Number : 45.876

Next Topic :