Difference Between Structure and Union in C
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 logically related data
items of various data types. Structures basically represent a record. All 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.
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.
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 of field.
• A Union or a Structure can easily pass by value to functions and 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.
Parameter Structure Union
Keyword A user can deploy the A user can deploy the
keyword struct to define a keyword union to define a Union.
Structure.
Internal The implementation of In the case of a Union, the
Implementa�on Structure in C occurs memory allocation occurs for
internally- because it contains only one member with the
separate memory locations largest size among all the input
allotted to every input variables. It shares the same
member. location among all these
members/objects.
Accessing A user can access individual A user can access only one
Members members at a given time. member at a given time.
Syntax The Syntax of declaring a The Syntax of declaring a Union
Structure in C is: in C is:
union [union name]
struct [structure name]
{
{
type element_1;
type element_1;
type element_2;
type element_2;
.
.
.
.
} variable_1, variable_2;
} variable_1, variable_2;
Size A Structure does not have a A Union does not have a
shared location for all its separate location for every
members. It makes the size of member in it. It makes its size
a Structure to be greater than equal to the size of the largest
or equal to the sum of the size member among all the data
of its data members. members.
Storage of Value In the case of a Structure, In the case of a Union, there is an
there is a specific memory allocation of only one shared
location for every input data memory for all the input data
member. Thus, it can store members. Thus, it stores one
multiple values of the various value at a time for all of its
members. members.
Ini�aliza�on In the case of a Structure, a In the case of a Union, a user can
user can initialize multiple only initiate the first member at a
members at the same time. time.