unions in c

the union is similar to structures. i.e. collection of different data types which are grouped together. Each element in a union is called member. but the difference is all members share the same location. structures allocate storage space for all its members separately. but union allocates one common storage for all its members. Since all members share one location they have common starting address. Because of the same memory we cant access all the members of the union at a time as structures.

the purpose of union is to prevent memory fragmentation by arranging standard size for datatype.by this no memory will be left empty. the freed memory will be reusable with another instance within the union.

declaration of the union:

union unionname {
datatype variable1;
datatype variable2;
...
};