Signup/Sign In

C Unions

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

Union and Structure comparison C language

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

Declaring a Union in C

A union is declared using the union keyword in C.

The syntax is as follows

union tag_name {
   member definition;
   member definition;
   ...
   member definition;
} union variable(s); 

For 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 of them can be used at a time. This is due to the fact that only one location is allocated for all the union variables, irrespective of their size. The compiler allocates the storage that is large enough to hold the largest variable type in the union.

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

To define variables of a union, we use union keyword as follows:

union item it2, it3;

Accessing a Union Member in C

We use member access operator (.) to access members of a union in C. It is used between the union variable name and the union member that we want to access. Syntax for accessing any union member is similar to accessing structure members.

union test
{
    int a;
    float b;
    char c;
}t;

t.a;    //to access members of union t
t.b;     
t.c;

In unions, if we change the value of any one member, the value of other members gets affected.


Using Union in C Program

Here is a program to understand how compiler decides size of a union:

#include <stdio.h>
  
union one {
    int x;
    char y;
} one1;
  
union two {
    int x;
    char y;
    long z;
} two2;
  
union three {
    int arr[100];
    char y;
    double d[5];
} three3;
  
int main()
{
    printf("sizeof(one) = %lu, sizeof(two) = %lu, sizeof(three) = %lu", sizeof(one1), sizeof(two2), sizeof(three3));
    return 0;
}


sizeof(one) = 4, sizeof(two) = 8, sizeof(three) = 400

Run Code →

Let's see another code example,

// defining and printing members of a union
#include <stdio.h>

union item
{
    int a;
    float b;
    char ch;
};

int main( )
{
    union item it;
    it.a = 12;
    it.b = 20.2;
    it.ch = 'z';
    
    printf("%d\n", it.a);
    printf("%f\n", it.b);
    printf("%c\n", it.ch);
    
    return 0;
}


1101109626
20.199940
z

We can also create an array of union type values. So let's see a code example,

#include <stdio.h>
union item
{
    int a;
    float b;
    char ch;
};

int main( )
{
    union item it[10];
    int n;
    printf("Enter the number of records:");
    scanf("%d", &n);
    for(int i = 0; i < n; i++) {
        printf("Enter record %d: ", i + 1);
        scanf("%d %f %c", &it[i].a, &it[i].b, &it[i].ch);
    }
    for(int i = 0; i < n; i++) {
        printf("\nRecord no. %d:\n", i + 1);
        printf("%d %f %c", it[i].a, it[i].b, it[i].ch);
    }
    
    return 0;
}


Enter the number of records:2
Enter record 1: 1
3
a
Enter record 2: 2
4
d

Record no. 1:
1077936225 3.000023 a
Record no. 2:
1082130532 4.000048 d

As you can see here, the values of int and float get corrupted and only char variable prints the expected result. This is because in union, the memory is shared among different data types.

In the above example, value of the char variable was stored at last, hence the value of other variables is lost.

Difference Between Structure and Union in C

  • In union, we can only initializa the first data member whereas in a structure, we can initialize many data members at once.
  • Compiler allocates memory for each member of a structure while for a union, it allocates memory equal to the size of the largest data member.
  • Union members share a memory location while structure members have a unique storage location each.
  • In a structure, we can access individual members simultaneously while in a union, we can only access one member at a time.
  • If we change the value of a member in a structure, it won't affect its other members but in a union, changing the value of one member will affect the others.

Difference in the size of Structure and Union in C

Let's look at the following example to understand better.

#include <stdio.h>
  
struct one {
    int x;
    char y;
    long z;
} one1;
  
union two {
    int x;
    char y;
    long z;
} two2;
  
int main()
{
    printf("sizeof(struct) = %lu, sizeof(union) = %lu",sizeof(one1), sizeof(two2));
    return 0;
}


sizeof(struct) = 16, sizeof(union) = 8

We can see that the size of structure is greater than the sum of size of its members while the size of union is equal to the size of its largest member. This is why there is a difference in the size of structure and union with exactly the same data members. Union members share memory while structure members have their own unique memory.