C Unions (original) (raw)

Last Updated : 24 Apr, 2025

In C, **union is a user-defined data type that can contain elements of the different data types just like structure. But unlike structures, all the members in the C union are stored in the same memory location. Due to this, only one member can store data at the given point in time.

Union Declaration

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

C `

union union_name{ type1 member1; type2 member2; type3 member3; . . . . }

`

After declaration of a union then create a variable of union like below:

C `

union_name variable_name;

`

We can also declare a variable at the declaration of union

C `

union union_name{ type1 member1; type2 member2; type3 member3; . . . . } variable_name;

`

Initialization and Accessing

The value of a union variable can be accessed using the **dot (.) operator. A value can be assigned to the union variable using the assignment **operator (=).

In a union, all the variables share the same memory, so only one variable can store a value at a time. If we try to access the value of another variable, the behavior will be undefined.

**Example:

C `

#include <stdio.h>

// Define a union with // different data types 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;

}

`

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. Let's take a look at the code example:

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.

**Syntax:

C `

union name1{ // Data members union name2{ // Data members }; };

`

Accessing members of union using dot(.) operator:

C `

outer.inner.innerMember

`

**Example:

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;

}

`

Anonymous Union

An anonymous union in C is a union that does not have a name. Instead of accessing its members through a named union variable, you can directly access the members of the anonymous union. This is useful when you want to access the union members directly within a specific scope, without needing to declare a union variable.

**Example:

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;

}

`

Structure vs Union

The following table lists the key **difference between the structure and union in C:

Structure Union
The size of the structure is equal to or greater than the total size of all of its members. The size of the union is the size of its largest member.
The structure can contain data in multiple members at the same time. Only one member can contain data at the same time.
It is declared using the struct keyword. It is declared using the union keyword.