Union in C Program



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,''AKKI'', ''mumbai''};

Accessing union members

data.id
data.name
data.address

Post a Comment

If you have any doubts, Please let me know
Thanks!

Previous Post Next Post