0% found this document useful (0 votes)
4 views3 pages

Difference Between Structure and Union in C 60

Structures and unions in C are both custom data types used to store different data types in a single entity, but they differ in memory allocation and data handling. Structures allocate separate memory for each member and allow access to all members simultaneously, while unions share a single memory location and can only hold one value at a time. The document also outlines the syntax for defining structures and unions, along with their similarities and key differences.

Uploaded by

Sheetal376
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Difference Between Structure and Union in C 60

Structures and unions in C are both custom data types used to store different data types in a single entity, but they differ in memory allocation and data handling. Structures allocate separate memory for each member and allow access to all members simultaneously, while unions share a single memory location and can only hold one value at a time. The document also outlines the syntax for defining structures and unions, along with their similarities and key differences.

Uploaded by

Sheetal376
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Difference Between Structure

and Union in C

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. The difference between structure and
union in the C programming language is explained in the table provided below:

Structure VS Union

Structure Union

The struct statement is used to define


Union keyword is used to define it.
it.

A separate memory location is


Single memory is allotted to all.
assigned

Change in one does not affect


Change in one affects other entities.
another.

They are used to store multiple


Stores on value at a time.
values.

The size of the structure is the sum of The size of the union is the size of the
all entities. largest entity.

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;
};

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

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.

The syntax to declare the union is shown below:

union [union name]


{
type member_1;
type member_2;
.

.
type member_n;
};

Key Difference Between Structure and Union in C


• In Structure, more than one member can be initialised simultaneously; in
Union, only the first member can be initialised at once.
• In Structure, any member can be retrieved or accessed by users at any time.
However, in Union, only one member can be accessed or retrieved at once.
• Every member of the input data has a specific memory location for a
Structure. As a result, it can store numerous values for each member. For all
the input data members in the case of a union, a single shared memory is
allocated. As a result, it saves a single value at a time for each member.
• The other members of the Structure are unaffected when one member's
values are changed. However, in Union, the values of other members are
impacted when you change the values of a single member.

Similarities Between Structure and Union in C

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.

You might also like