Unions In C Language
Unions are conceptually similar to structures. The syntax of union is also similar to that of structure. The only difference is in terms of storage. In structure each member has its own storage location, whereas all members of union use a single shared memory location which is equal to the size of its largest data member.
Unions

This implies that although a union may contain many members of different types, it cannot handle all the members at same time. A union is declared using union keyword.

Example :

      union item
      {
      int m;
      float x;
      char c;
      }It1;

This declares a variable It1 of type union item. This union contains three members each with a different data type. However only one member can be used 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 the storage that is large enough to hold largest variable type in the union.

In the union declared above the member x requires 4 bytes which is largest among the members in 16-bit machine. Other members of union will share the same address.

Accessing members of a union :

The member of unions can be accessed in similar manner as that structure. Suppose, you want to access x for union variable It1 in above example, it can be accessed as It1.c. If you want to access ‘c’ for union pointer variable ‘It1’, it can be accessed as (*It1).c or as It1c.

  Program : The following program is an example to access union members.
#include <stdio.h>
#include <string.h>
 
union Data
{
   int i;
   float f;
   char  str[20];
};
 
int main( )
{
   union Data data;        
   data.i = 10;
   data.f = 220.5;
   strcpy( data.str, :C Programming”);
   printf( “data.i : %d\n”, data.i);
   printf( “data.f : %f\n”, data.f);
   printf( “data.str : %s\n”, data.str);
  return 0;
}
Output :

data.i: 1917853763
data.f: 4122360580327794860452759994368.000000
data.str: C Programming

Here, we can see that values of i and f members of union got corrupted because final value assigned to the variable has occupied the memory location and this is the reason that the value if str member is getting printed very well. Now let's look into the same example once again where we will use one variable at a time which is the main purpose of having union.

  Program :
#include <stdio.h>
#include <string.h>
 union Data
{
   int i;
   float f;
   char str[20];
};
 int main( )
{
   union Data data;        
  data.i = 10;
   printf( “data.i : %d\n”, data.i);
   data.f = 220.5;
   printf( “data.f : %f\n”, data.f);
   strcpy( data.str, “C Programming”);
   printf( “data.str : %s\n”, data.str);
   return 0;
}
Output :

data.i: 10
data.f: 220.500000
data.str: C Programming

Here, all the members are getting printed very well because one member is being used at a time.

S.No C Structure                                   C Union
1 Structure allocates storage space for all its members separately. Union allocates one common storage space for all its members. Union finds that which of its member needs high storage space over other members and allocates that much space
Structure occupies higher memory space. Union occupies lower memory space over structure.
We can access all members of structure at a time. We can access only one member of union at a time.
Structure example :
struct student
{
int mark;
char name[6];
double average;
};
Union example :
union student
{
int mark;
char name[6];
double average;
};
For above structure, memory allocation will be like below.
int mark – 2 bytes
char name[6] – 6 bytes
double average – 8 bytes
Total memory allocation = 2+6+8 = 16 Bytes
For above union, only 8 bytes of memory will be allocated since double data type will occupy maximum space of memory over other data types. Total memory allocation = 8 Bytes

Though unions are similar to structure in so many ways, the difference between them is crucial to understand. This can be demonstrated by this example :

  Program :
#include <stdio.h>
union job	//defining a union
 {         
   char name[32];
   float salary;
   int worker_no;
}u;
struct job1
{
   char name[32];
   float salary;
   int worker_no;
}s;

int main(){
   printf(“size of union = %d”, sizeof(u));
   printf(“\n size of structure = %d”, sizeof(s));
   return 0;
}
Output :

size of union = 32
size of structure = 40

There is difference in memory allocation between union and structure as suggested in above example. The amount of memory required to store a structure variables is the sum of memory size of all members. But, the memory required to store a union variable is the memory required for largest element of a union.