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.
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.
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;
}
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;
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;
}
OutputSizeof 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
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;
}
Applications of Union in C
- Used for memory efficiency, particularly in embedded systems or situations where memory is a critical resource. By having different data types share the same memory, they reduce the overall memory footprint compared to a structure.
- Used to map to hardware registers that might have different interpretations depending on the context (e.g., a register that can be read as a status flag or written as a command).
- Suitable for representing data that can take on one of several different types but never simultaneously. For instance, a message packet might contain either an integer ID or a string message, but not both at once.
Next Read
Explore
C Basics
Arrays & Strings
Pointers and Structures
Memory Management
File & Error Handling
Advanced Concepts