What is the difference between structure and union in c

Learning with expert
3 min readFeb 12, 2021

--

Here in this article, we talk about the difference between structure and union.Both have different functions,memory,size,and storage.

difference between structure and union

1.Definition of structure and union

Structure: It is a user-defined data type in C language that combines related data items of different data types together. Structures are used to represent a Record.

Union: union is a special data type in C that works to allow storing different data types in the same memory location.

2.Syntax of structure and Union

Syntax of Structure: structure defined with struct statement.

Struct [structure name]

{

type element 1;

— — — — — — — — — 2;

— — — — — — — — — 3;

};

Syntax of Union: Union is used as a union statement, just like in structure.

union [union name]

{

type element 1;

— — — — — — — — — 2;

— — — — — — — — — 3;

};

3.Size of structure and union:

Structure: structure does not have a shared location for its members so the size of the Structure is equal or greater than the sum of size of all the data members.

Union: Union does not have a separate location for each of its members so its size or equal to the size of the largest member among all data members.

4.Memory of structure and Union:

Structure: Each member within a structure is assigned a unique storage area of location.

Union: Memory allocated is shared by individual members of the union.

5.Value storage of structure and union:

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

Union: While in the case of 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.

6.Initialization of Structure and union:

Structure: Several members of a structure can initialize at once.

Union: Only the first member of a union can be initialized.

C structures and unions Advantages

Advantages of structure in C programming:

  1. Structures gather more than one piece of data about the same subject together in the same place.

2. It is helpful when you want to gather the data of similar data types and parameters like first name, last name, etc.

3. It is very easy to maintain as we can represent the whole record by using a single name.

4. In structure, we can pass a complete set of records to any function using a single parameter.

5. You can use an array of structures to store more records with similar types.

Advantages of the union in C programming:

  1. It occupies less memory compared to the structure.
  2. When you use union, only the last variable can be directly accessed.
  3. Union is used when you have to use the same memory location for two or more data members.
  4. It enables you to hold data of only one data member.
  5. Its allocated space is equal to the maximum size of the data member.

--

--