Union in C


Reading time: 20 minutes

A union is a special data type available in C that allows to store 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.

Syntax :

union [union tag]
{
   member definition;
   member definition;
   ...
   member definition;
} [one or more union variables];  

Create union variables and access members of a union

When a union is defined, it creates a user-defined type. However, no memory is allocated. To allocate memory for a given union type and work with it, we need to create variables.

Here's how we create union variables:

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

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

Another way of creating union variables is:

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

In both cases, union variables car1, car2, and a union pointer car3 of union car type are created.

How to access members of a union?

We use '.' to access normal variables of a union. To access pointer variables, we use -> operator.

In the above example,

price for car1 can be accessed using car1.price
price for car3 can be accessed using car3->price

Similarities between struct and union

  1. Both are user-defined data types used to store data of different types as a single unit.
  2. Their members can be objects of any type, including other structures and unions or arrays. A member can also consist of a bit field.
  3. Both structures and unions support only assignment = and sizeof operators. The two structures or unions in the assignment must have the same members and member types.
  4. A structure or a union can be passed by value to functions and returned by value by functions. The argument must have the same type as the function parameter. A structure or union is passed by value just like a scalar variable as a corresponding parameter.
  5. ‘.’ operator is used for accessing members.

Difference between struct and union

keyword

  • struct keyword is used to declare the structure

  • union keyword is used to declare the Union

Memory allocation

  • Structure variable will allocate memory for all the structure member separately.

  • Union variable will allocate common memory for all the union members.

Example

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

Memory Space

  • Structures will occupy more memory space.Memory_Size = addition of all the structure members sizes.Memory_Size = int + char array [50] + floatMemory_Size = 2 + 50 + 4 BytesMemory_Size = 56 Byte.

  • Union will occupy less memory space compared to structures.Memory_Size = Size of the largest Union member. From the above example, Largest Union member is char array so, Memory_Size = 50 Bytes.

Access

  • It allows us to access any or all the members at any time. * It allows us to access only one union member at a time

Example

#include <stdio.h> 
  
union test1 
{ 
    int x; 
    int y; 
} Test1; 
  
union test2 
{ 
    int x; 
    char y; 
} Test2; 
  
union test3 
{ 
    int arr[10]; 
    char y; 
} Test3; 
  
int main() 
{ 
    printf("sizeof(test1) = %lu, sizeof(test2) = %lu, "
           "sizeof(test3) = %lu", 
           sizeof(Test1), 
           sizeof(Test2), sizeof(Test3)); 
    return 0; 
} 

Output :

sizeof(test1) = 4, sizeof(test2) = 4, sizeof(test3) = 40

Question

Consider the following C code:

union test
{
    int x;
    char arr[8];
    int y;
};
 
int main()
{
    printf("%d", sizeof(union test));
    return 0;
}

What will be the output of the above C code?

8
12
6
compiler error
When we declare a union, memory allocated for a union variable of the type is equal to memory needed for the largest member of it, and all members share this same memory space. In above example, "char arr[8]" is the largest member. Therefore size of union test is 8 bytes.