Union in C ?


A union is a derived data type like structure, so union also can store different data types.


Syntax:-
union tag_name
{
type 1;
type 2;
}variable;


Structure Union
The keyword struct is used to define a structure. The keyword union is used to define a union.
When a variable is associated with a structure the compiler allocates the memory for each member. When a variable is associated with a union, the compiler allocates the memory by considering the size of the largest member. So size of union is equal to the size of largest member.
The size of structure is greater than or equal to sum of size of its members may end with unused slack bytes. Memory allocated is shared by individual members of union.
Each member within a structure is assigned unique storage area. The address is same for all the members of a union indicates that every members of union.