C 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 the 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 :


Initializing and Declaring union variable

Accessing union members

In the above example, the data that is the ‘id’, ‘name’, ‘address’ are accessed by using member access operator (.)

Following examples shows the use of union:

The output of the above program: