Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests - Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests -

Difference Between Structure and Union in C

There are five ways that the C language provides for creating custom data. They are bit-field, structure, typedef, enumeration, and union. In this article, we will dig deeper into the difference between Structure and Union in C. They are both container data types, and they are capable of holding any data type. Although there is one very major difference between them. The union has the same memory location for all of its members, while the Structure possesses separate ones for the same purpose. Let us understand more differences between them.

Ultimate Guide to Kickstart your GATE Exam Preparation
Download the e-book now

GATE Rank Predictor

What is a Structure in C?

A Structure is a type of data that is user-defined. It is available in the C programming language that allows a user to combine together logically related data items of various data types. Structures basically represent a record. All of its elements get stored in the contiguous memory locations. A Structure type- the Variables- can store more than one data item from raging data types all under one name.

Difference Between Structure and Union in C PDF
Download Full PDF

Defining a Structure – A user must deploy the struct statement to define a structure. This statement determines a new data type. It has equal to or more than one member. Here’s the format of a struct statement:

Syntax of Declaring a Structure

struct [structure name]

{

type member_1;

type member_2;

. . .

type member_n;

};

What Is a Union?

A Union is a type of data that is user-defined. It is just like the structure. The Union combines various objects of different sorts and sizes together. A user can define a Union using many members, but only one of them holds a value at any given time. It provides you with an efficient way of using a single memory location for various purposes. Thus, varying objects can share a similar location.

Defining a Union – A user must deploy the union statement for defining a Union. It determines a new data type that has more than one member for the intended program. Here’s the format of a union statement:

Syntax of Declaring a Union

union [union name]

{

type member_1;

type member_2;

. . .

type member_n;

};

Functions and Similarities Between Union and Structure in C

  • They are both user-defined data types, and they store different sorts of data together as a single unit.
  • Both of their members can be any type of object. It may include different structures and unions/ arrays. Its members can also contain a bit field.
  • A Union or a Structure can easily pass by value to functions and also return to the value by functions. Every argument must possess the same parameters as that of the function parameter.
  • A Union or Structure passes by the value just like any scalar variable in the form of a corresponding parameter.
  • You can use the “.” operator for accessing the members.

Difference Between Structure and Union in C

Parameter Structure Union
Keyword A user can deploy the keyword struct to define a Structure. A user can deploy the keyword union to define a Union.
Internal Implementation The implementation of Structure in C occurs internally- because it contains separate memory locations allotted to every input member. In the case of a Union, the memory allocation occurs for only one member with the largest size among all the input variables. It shares the same location among all these members/objects.
Accessing Members A user can access individual members at a given time. A user can access only one member at a given time.
Syntax The Syntax of declaring a Structure in C is:

struct [structure name]

{

type element_1;

type element_2;

.

.

} variable_1, variable_2, …;

The Syntax of declaring a Union in C is:

union [union name]

{

type element_1;

type element_2;

.

.

} variable_1, variable_2, …;

Size A Structure does not have a shared location for all of its members. It makes the size of a Structure to be greater than or equal to the sum of the size of its data members. A Union does not have a separate location for every member in it. It makes its size equal to the size of the largest member among all the data members.
Value Altering Altering the values of a single member does not affect the other members of a Structure. When you alter the values of a single member, it affects the values of other members.
Storage of Value In the case of a Structure, there is a specific memory location for every input data member. Thus, it can store multiple values of the various members. In the case of a Union, there is an allocation of only one shared memory for all the input data members. Thus, it stores one value at a time for all of its members.
Initialization In the case of a Structure, a user can initialize multiple members at the same time. In the case of a Union, a user can only initiate the first member at a time.

Keep learning and stay tuned to BYJU’S to get the latest updates on GATE Exam along with GATE Eligibility Criteria, GATE 2024, GATE Admit Card, GATE Application Form, GATE Syllabus, GATE Cutoff, GATE Previous Year Question Paper, and more.

Also Explore,

Comments

Leave a Comment

Your Mobile number and Email id will not be published.

*

*