Union in C

tutorial and example
tutorial and example

Union is a user defined data type that is used to hold the different types of elements like structure. In union all members share the same memory location.

Syntax:

Example

Difference between C structure and C union

C Structure                              C Union
It allocates storage space for all its members separately. It allocates one common storage space for all its.
It occupies height memory space. It occupies lower memory space over structure.
All the member can be access at a time Only one member can be acces at a time
Example:

struct employe

{

int id;

char name[40];

float salary;

};

Example:

union employe

{

int id;

char name[40];

float salary;

};