Difference Between Union and structure in C programming Language.

From the given fig. let us understand some of the differences in detail and then let list out all differences between structure and union in tabular format.
A programmer must use struct statement for defining structure and the same way for defining union must use union statement.
Structure and Union they both are user defined data types and used to store different types of data together as single unit.
From fig. we can understand that the structure occupies separate or individual memory(location) for its members.In other word we can say taht a Structure does not have a shared memory location for all of its members.
The size of structure is equal to the sum of the its all data memebers.The size occupied by structure is 28 bytes.
On Other hand unlike structure a Union does not have a separate memory location for every member in it.The size of union is the size equal to the size of the largest member among all the data members in the union. The size occupied by union is 20 bytes.
The programmer can access individual or all members of structure at a time but where as in union the user access only one member at given point of time.
Let us list out all differences between structure and union in tabular format.


Structure

Union

The struct keyword is used to define or declare structure.

The Union keyword is used to define a Union.

A user or programmer can access individual or all members of structure at a given time.

A user or programmer can access only one member at a given point of time.

The Structure implementation in C is internal- because it allocate or occupies separate memory locations to every input member.

In union the memory allocation occure for the member who has the largest size among all the members.

Syntax: a Structure Declaration
struct [Name of Structure]
{
 type member_1;
 type member_2;
 .
 .
} variable1, variable2, …;

Syntax:a Union Declaration:
union [Name of Union]
{
 type member_1;
 type member_2;
 .
 .
} variable1, variable2, …;

The size of structure is equal to the sum of the size of its data members.

The size of Union is equal to the size of the largest data member among all the data members.

A Structure does not have a shared location for all of its members.

A Union does not have a separate location for every member.

Modification or change in the values of a single member of a structure does not affect the other members

Modification or change in the values of a single member of a Union, it affects the values of other members.

At the same time a user can initialize multiple members.

a user can only initialize the only one member at a time.

In a Structure, individual or separate memory location is allocated for every input data member. So That, it can store multiple values of the various data types

In a Union, there is an allocation of only one shared memory for all the data members. Thus, it stores one value at a time for all of its data members.

e.g
struct employee
{
 int empno;
 char ename[20];
 long salary;
};

e.g
union employee
{
 int empno;
 char ename[20];
 long salary;
};



Previous Topic:-->> Create and Use Union in C. || Next topic:-->>Union Examples.