Difference between Structure and Union in C Program


In C, we have containers to hold data of same data type as well as different data types. C provides the concept of Arrays to store data variables of same type; while for storing data of different types, C has the concept of structures and unions.

Both Structures and Unions can hold different types of data, but on the basis of their internal implementation, we can find several differences in both of these containers. Read this article to learn more about structures and unions and how they are different from each other.

What is Structure in C Program?

In C programming, a Structure is a userdefined datatype. It is basically used to combine different datatypes into a single datatype. A structure in a C program can contain multiple members and structure variables. In order to define structures, the 'struct' keyword is used. To access the members of a structure, we use the dot (.) operator.

Syntax

The syntax of structures in C language is,

struct structure_name {
   member definition;
} structure_variables;

Where,

  • structure_name is the name given to the structure.

  • member definition is the set of member variables.

  • structure_variable is the object of structure.

Example

struct Data {
   int a;
   long int b;
} data, data1;

What is Union in C Program?

In C programming, a union is also a user-defined datatype. All the members of a union share the same memory location. Therefore, if we need to use the same memory location for two or more members, then union is the best data type for that. The largest union member defines the size of the union.

In C programming, unions are similar to structures, as the union variables are also created in the same manner as the structure variables. To define a union in a C program, the keyword "union" is used.

Syntax

The syntax of unions in C language is,

union union_name {
   member definition;
} union_variables;

Where,

  • union_name is any name given to the union.

  • member definition is the set of member variables.

  • union_variable is the object of union.

Example

union Data {
   int i;
   float f;
} data, data1;

Difference between Structure and Union

The following are the important differences between Structure and Union −

Key

Structure

Union

Definition

A Structure is a container defined in C to store data variables of different type and also supports for the userdefined variables storage.

A Union is also a similar kind of container in C which can hold different types of variables along with the userdefined variables.

Internal implementation

Structures in C are internally implemented. There is separate memory location allotted for each input member.

While in case Union memory is allocated only to one member having largest size among all other input variables and the same location is being get shared among all of these.

Syntax

Syntax of declare a Structure in C is as follows −

struct struct_name{
   type element1;
   type element2;
   .
   .
} variable1, variable2, ...;

The syntax of declare a Union in C is as follows

union u_name{
   type element1;
   type element2;
   .
   .
} variable1, variable2, ...;

Size

Structures do not have shared location for their members, so the size of a Structure is equal or greater than the sum of size of all the data members.

Unions do not have separate locations for each of their members, so their size or equal to the size of largest member among all data members.

Value storage

In case of a Structure, there is specific memory location for each input data member and hence it can store multiple values of the different members.

In a Union, there is only one shared memory allocation for all input data members so it stores a single value at a time for all members.

Initialization

In a Structure, multiple members can be can be initializing at same time.

In a Union, only the first member can get initialize at a time.

Conclusion

Both structures and unions are composite data types in C programming. The most significant difference between a structure and a union is the way they store their data. A structure stores each member in separate memory locations, whereas a union stores all its members in the same memory location.

Updated on: 22-Feb-2023

26K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements