Difference Between Structure and Union in C

By Mohit Uniyal|Updated : April 20th, 2022

Difference Between Structure and Union in C: Structure and union are the ways of creating custom data in the C programming language. It offers various types of built-in data. The major difference between structure and union in C language is that to define structure we use struct statement and to define union we use union statement. 

The union and structure are used to combine the various objects belonging to the same memory location. Before learning about the difference between structure and union in C programming language we will first see a brief introduction to structure and union individually.

Table of Content

What is a Structure in C?

A structure in the C programming language is a custom data type. They are used to hold the different data types. The elements stored in the structure can be accessed and retrieved instantly. The struct statement is used to define the structure. 

The syntax to declare the structure is shown below:

 struct [structure name]
{
type member_1;
type member_2;
.

.

.
type member_n;
};

What is Union in C?

A union is the C programming language is also a custom data type. They are also known as user-defined data types. Various union members could be defined at once but only one will hold a value. The union statement is used to define the union.

The syntax to declare the union is shown below:

union [union name]
{
type member_1;
type member_2;
.

.

.
type member_n;
};

What is the Difference Between Structure and Union in C?

Apart from various similarities between structure and union such as both are custom data types and solve the same purpose of storing different types of data in a single entity. There are a few differences between the two. The difference between structure and union in the C programming language is explained in the table provided below:

Difference Between Structure and Union in C
StructureUnion
The struct statement is used to define it.Union keyword is used to define it.
A separate memory location is assignedSingle memory is allotted to all.
Change in one does not affect another.Change in one affects other entities.
They are used to store multiple values.Stores on value at a time.
The size of the structure is the sum of all entities.The size of the union is the size of the largest entity.

Comments

write a comment

FAQs

  • The major difference between structure and union in C is that structure uses a separate location to store the entities whereas union uses the same location to save all the entities. The change in one entity stored via union will affect all the other entities.

  • A structure in C is used to store a variety of data types. It is a type of custom data. A struct statement is used to define it. The syntax of the structure is:

    struct [structure name]

    {

    type member_1;

    type member_2;

    .


    .


    .

    type member_n;

    };

  • A union in C programming language is another custom data type similar to structure. They are also used to store different data types. The syntax of union is:

    union [union name]

    {

    type member_1;

    type member_2;

    .

    .

    .

    type member_n;

    };

  • As per the difference between structure and union in C with respect to the size of the custom memory is that size of the structure is the sum of size of each element in it whereas in the union it is the size of the largest entity.

  • The size of structure in C programming language 8 bytes. It is the sum of the size of the all entities saved in the structure.

Follow us for latest updates