• admin@embedclogic.com

C -Union


Union is also a collection of heterogeneous data types like structure but it differs in memory allocation to its members.

Union reserves memory equals to the size of its largest member and this memory is shared by all of its members.


where tag_name is optional.


sizeof(emp)=20 byte(name is the largest member of union/without alignment)

As 20 byte is shared between all three members name,empId and salary so you can get save only one member at a time and get it correctly.

If you will try to save all 3 members simultaneously in memory as we do in structure then latest one will overwrite to old one.See below program to understand the overwrite concept –


You Can see empId and salary has some garbage value as it was overwritten by name.


— union size is 24 because of alignment.

— You can not get the address of union members as it is shared between all members.

Except memory sharing difference union is almost same as structure so all the concept which you have seen in structure will apply for union also.