All the structure elements are stored at contiguous memory locations. Structure type variable can store more than one data item of varying data types under one name. In this tutorial, you will learn:

What is Structure? What is Union Syntax of Declaring Structure Example of Structure in C Programming Syntax of Declaring Union Example of Union in C Programming Structure Vs. Union Advantages of structure Advantages of union Disadvantages of structure Disadvantages of union

What is Union

Union is a user-defined data type, just like a structure. Union combines objects of different types and sizes together. The union variable allocates the memory space equal to the space to hold the largest variable of union. It allows varying types of objects to share the same location.

Syntax of Declaring Structure

Structure is declared using the “struct” keyword and name of structure. Number 1, number 2, number 3 are individual members of structure. The body part is terminated with a semicolon (;).

Example of Structure in C Programming

In the above program, a structure called student is created. This structure has three data members: 1) name (string), 2) roll_no (integer), and 3) marks (float). After this, a structure variable sdt is created to store student information and display it on the computer screen.

Output:

Enter the following information: Enter student name: James Enter student roll number: 21 Enter student marks: 67 The information you have entered is: Student name: John Student roll number: 21 Student marks: 67.0

Syntax of Declaring Union

Union is declared using the “union” keyword and name of union. Number 1, number 2, number 3 are individual members of union. The body part is terminated with a semicolon (;).

Example of Union in C Programming

Output:

1101109601 20.199892 a In the above program, you can see that the values of x and y gets corrupted. Only variable ch prints the expected result. It is because, in union, the memory location is shared among all member data types. Therefore, the only data member whose value is currently stored, will occupy memory space. The value of the variable ch was stored at last, so the value of the rest of the variables is lost.

Structure Vs. Union

Structure Vs. Union Here is the important difference between structure and union:

Advantages of structure

Here are pros/benefits for using structure:

Structures gather more than one piece of data about the same subject together in the same place. It is helpful when you want to gather the data of similar data types and parameters like first name, last name, etc. It is very easy to maintain as we can represent the whole record by using a single name. In structure, we can pass complete set of records to any function using a single parameter. You can use an array of structure to store more records with similar types.

Advantages of union

Here, are pros/benefits for using union:

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

Disadvantages of structure

Here are cons/drawbacks for using structure:

If the complexity of IT project goes beyond the limit, it becomes hard to manage. Change of one data structure in a code necessitates changes at many other places. Therefore, the changes become hard to track. Structure is slower because it requires storage space for all the data. You can retrieve any member at a time in structure whereas you can access one member at a time in the union. Structure occupies space for each and every member written in inner parameters while union occupies space for a member having the highest size written in inner parameters. Structure supports flexible array. Union does not support a flexible array.

Disadvantages of union

Here, are cons/drawbacks for using union:

You can use only one union member at a time. All the union variables cannot be initialized or used with varying values at a time. Union assigns one common storage space for all its members.

Also Check our C Tutorial for Beginners:- Click Here