Union
C Union is also like structure, i.e. collection of different data types which are grouped together. Each element in a union is called member.
Union and structure both are same, except allocating memory for their members. Structure allocates storage space for each member separately. Whereas, Union allocates one common storage space for all its members.
Syntax:
union tag_name
{
data type var_name1;
data type var_name2;
data type var_name3;
};
Example :
union student
{
int id;
char name[10];
char address[50];
};
Initializing and Declaring union variable:
union student data;
union student data = {001,''Anshul'', "Nagpur"};
Accessing union members:
data.id
data.name
data.address