Unions in C

Last Updated : 20 Jul, 2026

A union is a user-defined data type that allows storing different data types in the same memory location. At any given time, only one member can hold a valid value because all members share the same memory.

  • All union members share the same memory location, so updating one member overwrites the others.
  • Unions are useful for saving memory when only one of several data members is needed at a time.
C
#include <stdio.h>

union Student {
    int rollNo;
    float height;
    char firstLetter;
};

int main() {
    
    // Declare a union variable
    union Student data;

    // Assign and print the roll number
    data.rollNo = 21;
    printf("%d\n", data.rollNo);
    data.height = 5.2;
    printf("%.2f\n", data.height);
    data.firstLetter = 'N';
    printf("%c", data.firstLetter);

    return 0;
}

Output
21
5.20
N

Union Declaration

A union is declared similarly to a structure. Provide the name of the union and define its member variables.

union UnionName {
data_type member1;
data_type member2;
...
};

Size of Union

The size of the union will always be equal to the size of the largest member of the union. All the less-sized elements can store the data in the same space without any overflow.

C
#include <stdio.h>

union A{
    int x;
    char y;
};

union B{
    int arr[10];
    char y;
};


int main() {

    // Finding size using sizeof() operator
    printf("Sizeof A: %ld\n", sizeof(union A));
    printf("Sizeof B: %ld\n", sizeof(union B));
    return 0;
}

Output
Sizeof A: 4
Sizeof B: 40

The above unions' memory can be visualized as shown:

Nested Union

In C, we can define a union inside another union like structure. This is called nested union and is commonly used when you want to efficiently organize and access related data while sharing memory among its members.

C
#include <stdio.h>

// Define a union with 
// different data types
union Student {
    int rollNo;
    union Academic{
        int marks;
    } performance;
};

int main() {
    
    // Declare a union variable
    union Student abc;

    // Assign and print the 
    // roll number
    abc.rollNo = 21;
    printf("%d\n", abc.rollNo);
    
    // Assign and print the 
    // member of inner union
    abc.performance.marks = 91;
    printf("%d", abc.performance.marks);
    return 0;
}

Output
21
91

Anonymous Union

An anonymous union is a union declared without a name, allowing its members to be accessed directly without using a union variable. It is useful when you need direct access to union members within a specific scope while still sharing the same memory location.

C
#include <stdio.h>

// Define a union with 
// different data types
struct Student {
    int rollNo;
    
    // Anonymous union
    union {
        int marks;
    } performance;
};

int main() {
    
    // Declare a structure variable
    struct Student abc;

    abc.rollNo = 21;
    printf("%d\n", abc.rollNo);
    
    // Assign and print the member of anonymous union
    abc.performance.marks = 91;
    printf("%d", abc.performance.marks);

    return 0;
}

Output
21
91

Applications of Union in C

Unions are commonly used when you want to save memory by storing different types of data in the same memory location.

  • They reduce memory usage by allowing multiple data types to share the same memory space.
  • They are useful for hardware programming, such as representing hardware registers in different ways.
  • They are ideal when only one of several data types is needed at a time, such as storing either an ID or a string.
Comment