Difference Between Structure and Union in C

By Mohit Uniyal|Updated : May 4th, 2023

The key difference between a structure and a union in C is the way they allocate memory. 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 the structure, we use the struct statement, and to define union, we use the union statement.

Difference Between Structure and Union in C (Downloadable PDF)

There are various differences between Structure and Union in C based on parameters such as size, storage value, syntax, etc. The union and structure combine the various objects belonging to the same memory location. Let us learn about the difference between structure and union in C programming language, along with a brief introduction to structure and union in C programming language individually.

Table of Content

Difference Between Structure and Union

Structures and unions are commonly used in programming when dealing with complex data types or when there is a need to group related data together. Apart from various similarities between structure and union, both are custom data types and solve the same purpose of storing different data types in a single entity. There are a few differences between the two as well.

The difference between structure and union in the C programming language is explained in the table provided below:

Structure VS Union
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.

Example to Show Difference Between Structure and Union

In the example shown below, we define a structure ExampleStruct that contains three member variables of different types. We also define a union ExampleUnion that contains the same three member variables. In the main function, we create variables of both types and print their sizes using the sizeof operator.

Here is an example to illustrate the difference between a structure and a union:

byjusexamprep

The output of this program would be:

Size of ExampleStruct = 12 bytes
Size of ExampleUnion = 4 bytes

As you can see, the size of the structure is the sum of the sizes of its member variables (4 bytes for var1, 4 bytes for var2, and 1 byte for var3), which equals 12 bytes. The size of the union, on the other hand, is only 4 bytes because it only needs to allocate enough memory for its largest member variable (var1).

Structure and Union

In C, both structure and union are used to define custom data types that can hold multiple variables of different types. Structures and unions are versatile data types that can be used in a wide range of programming applications to improve data organization, reduce memory usage, and simplify data access.

What is a Structure in C?

A structure in the C programming language is a custom data type. They are used to hold different data types. The elements stored in the structure can be accessed and retrieved instantly. Various linear data structures are used to store and organize data, such as stack and queue. The difference between Stack and Queue is essential to understanding the structure in C. 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 and 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.

Syntax to declare the union is shown below:

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

.

.
type member_n;
};

Also check: Candidates can also learn about the difference between Structure and Class in C++ here.

Similarities Between Structure and Union

The following are some similarities between union and structure:

  • The custom data types union and structure both store several sorts of data collected as a single entity.
  • Members of structures and unions can be any sort of object, including arrays, unions, and other structures.
  • Both structures and unions are capable of being supplied as values to functions and having their values returned to them.
  • The function parameter's type must match that of the argument.

Check out a few important related topics to the difference between Structure and Union in C in the table provided below:

Difference Between RAM and ROMDifference Between Calloc () and Malloc ()
Difference Between Hard Copy and Soft CopyDifference Between Encoder and Decoder
Difference Between MAC Address and IP AddressDifference Between Symmetric and Asymmetric Key Encryption

Comments

write a comment

FAQs on Difference between Structure and Union

  • 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