What is Union in C ?
In C, a union is a user-defined data type that allows you to store different data types in the same memory location. This is achieved by allocating memory equal to the size of the largest data type in the union. A union can have multiple members, but only one member can have a value at any given time. Unions are a convenient technique to use the same memory region for multiple purposes. In structure the size of a structure is the sum of the sizes of its members, while the size of a union is the size of its largest member. In Union memory is shared between the members of the union.
Syntax for Defining a Union in C
union union_name
{
data_type member1;
data_type member2;
...
};
Here union_name
is the name of the union, and member1
, member2
, etc. are the different data types that can be stored in the union.
For example, to define a union called "student" that can store an int, a float, and a char, you would write:
union student
{
int i;
float f;
char c;
};
You can also define an instance of union and access the member like this :
union student s1;
s1.i = 10;
printf("%d",s1.i);
Example program that demonstrates the use of a union in C
#include <stdio.h>
union student
{
int i;
float f;
};
int main()
{
union student s1;
// Assign an int value to the union
s1.i = 10;
printf("Integer value: %d", s1.i);
// Assign a float value to the union
s1.f = 3.14;
printf("Float value: %f", s1.f);
return 0;
}
In this program, we define a union called "student" that can store an int, a float, and a char. We then create an instance of the union called "s1". We then assign an int value, a float value, and a char value to the union and print out the values. Because a union uses the same memory location for all of its members, the output will be the last value assigned to the union.
Advantages of Union
There are several advantages to using unions in C:
-
Memory efficiency: Because a union uses the same memory location for all of its members, it can be a more memory-efficient way to store data compared to using separate variables for each type of data.
-
Improved code readability: Unions can make code more readable by allowing different types of data to be grouped together and accessed in a logical and organized way.
-
Space optimization: Unions can be useful for space optimization, as it only uses the amount of memory required for the largest member.
-
Flexibility: Unions provide a great deal of flexibility, as it can be used to store different data types, and only one member can use memory at a time.
What is difference between structure and union in C?
Here are some key differences between structures and unions in C:
-
Memory allocation: Structures use separate memory locations for each member, while unions use the same memory location for all members.
-
Size: The size of a structure is the sum of the sizes of its members, while the size of a union is the size of its largest member.
-
Accessing members: In a structure, all members can be accessed independently, while in a union, only one member can be accessed at a time, and accessing one member will overwrite the previous value stored in other members.
-
Use case: Structures are useful when a group of variables with different data types need to be stored together and accessed independently, while unions are useful when a group of variables with different data types need to be stored together, but only one is needed to be accessed at a time.
-
Syntax: Structures are defined using the
struct
keyword, while unions are defined using theunion
keyword. -
Initialization : Structures can be initialized using curly braces { } and comma separation, while Unions can be initialized only by assigning values to the member.