Table of Contents
C Unions
Unions are user-defined data types that, like structures, can contain elements of different data types. However, there is a key distinction between structures and unions. In a structure, each member has its own memory location, and all members can store data simultaneously in memory. This means you can access and manipulate each member independently.
In a union, all members share the same memory location. As a result, only one member can store data at a given instance. When you assign a value to one member, it will overwrite the data stored in other members of the union.
Unions are often used in cases where you want to represent the same memory location in multiple ways, depending on which member of the union is currently active or “selected.” This can be useful for memory-efficient representation of data where only one representation is valid at a time.
Syntax of Union in C
C Union Declaration
When you declare a union in C, you are essentially defining the template or structure of the union. This declaration specifies the names of the members, their data types, and the name of the union itself, but it does not allocate memory for the union at that point.
Memory for the union is allocated when you create an instance of the union. In other words, you declare variables of the union type, and it’s at that point that memory is allocated for the union and its members. This deferred memory allocation is a characteristic shared with other C data types, such as structures.
union union_name {
datatype member1;
datatype member2;
...
};
Different Ways to Define a Union Variable
We have to define a variable of the union type to start using union members. The two methods which are used to define a union variable.
- With Union Declaration
- After Union Declaration
1. Defining Union Variable with Declaration
union union_name {
datatype member1;
datatype member2;
...
} var1, var2, ...;
2. Defining Union Variable after Declaration
union union_name var1, var2, var3...;
Access Union Members
The members of union can be accessed using the (.) dot operator.
var1.member1;
where var1 is referred to as the union variable and member1 is the member of the union.
The above method is mainly used for accessing the members of the union and also works for the nested unions.
var1.member1.memberA;
- var1 is referred as union variable.
- member1 is known as a member of the union.
- memberA is a member of member1.
Initialization of Union in C
The initialization of a union is done by initialization its members and, then simply assigning the value to it.
var1.member1 = some_value;
Example
// C Program to demonstrate how to use union
#include <stdio.h>
// union template or declaration
union un {
int member1;
char member2;
float member3;
};
// driver code
int main()
{
// defining a union variable
union un var1;
// initializing the union member
var1.member1 = 15;
printf("The value stored in member1 = %d",
var1.member1);
return 0;
}
Output
The value stored in member1 = 15
Size Of Union
The size of a union always matches the size of its largest member. The smaller members within the union can coexist within the same memory space without any potential for overflow.
// C Program to find the size of the union
#include <stdio.h>
// declaring multiple unions
union test1 {
int x;
int y;
} Test1;
union test2 {
int x;
char y;
} Test2;
union test3 {
int arr[10];
char y;
} Test3;
// driver code
int main()
{
// finding size using sizeof() operator
int size1 = sizeof(Test1);
int size2 = sizeof(Test2);
int size3 = sizeof(Test3);
printf("Sizeof test1: %d\n", size1);
printf("Sizeof test2: %d\n", size2);
printf("Sizeof test3: %d", size3);
return 0;
}
Output
Sizeof test1: 4
Sizeof test2: 4
Sizeof test3: 40
Difference between C Structure and C Union
Structure | Union |
---|---|
The size of the structure is however 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 contains data from multiple members at the same time. | Only one member will contain data at the same time. |
Structure is even declared using the struct keyword. | Union is declared using the union keyword. |
FAQ- C Unions
Q1. What are unions in C?
Ans. A union is a custom data type in C programming. It allows you to group variables of different data types within the same memory location. While you can define a union with multiple members, it’s important to note that, at any specific instance, only one of its members can store a value. This enables unions to efficiently share memory among various data types.
Q2. What is a union vs struct in C?
Ans. A union is similar to a struct in that it contains multiple fields (members), and by default, these fields are public. However, the key difference is that, in a union, only one of its fields is used at any given time, and these fields share the same storage space. This flexibility allows a union to store values of different data types at different times, making it a valuable tool for efficient memory usage when you need to represent various data types in a single memory location.
Q3. What is union in function?
Ans. The union of two sets A and B is a new set that contains all the elements present in both sets A and B. Importantly, it ensures that no element is repeated; each element appears in the resulting union set only once. This operation in set theory is commonly denoted by the symbol ∪ and is a fundamental concept when working with sets.
Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together