Difference between union and structure


In C programming, both structure and union are user defined data types that hold multiple members of different data types.

Structures are used when we need to store distinct values for all the members in a unique memory location while unions help to manage memory efficiently.

Major difference between a structure and a union is storage.

In a structure each member has its distinct storage location while the members of a union utilize a shared memory location that is equal to the size of its largest data member.

Declaring structure

struct struct_sample
{
    int integer;
    float decimal;
    char name[20];
};

Declaring union

  
union union_sample
{
    int integer;
    float decimal;
    char name[20];
};

👈       👉