Compare the Difference Between Similar Terms

Difference Between Structure and Union in C

Key Difference – Structure vs Union in C
 

An array is a data structured supported by C language. An array can be used to store data elements of the same type. If there is a statement as int marks [10]; then marks are an array that can store ten marks and all of them are integers. Sometimes it is required store data elements of different types in the same memory location. For example, an employee can have an employee ID, name, department, age etc. They are of different data types. Therefore, it is necessary to have a method to store various data elements as a single unit. Structures and Unions in C are used for storing data elements of different types in the same memory location.A structure and a union are similar but they mainly differentiate due to memory allocation. The memory required to store a structure variable is the summation of the memory size of all members. The memory required to store a union variable is the memory required for the largest element in the union. That is the key difference between structure and union in C. This article discusses the difference between structure and union in C.

CONTENTS

1. Overview and Key Difference
2. What is Structure in C
3. What is Union in C
4. Similarities Between Structure and Union in C
5. Side by Side Comparison – Structure vs Union in C in Tabular Form
6. Summary

What is Structure in C?

A structure is a user-defined data type in C. It helps to combine data items of different types. A structure can represent records. A student can have student_id, student_name etc. Rather than storing each variable separately, all these different data items can be compact into a single unit using a structure. It is defined using the keyword ‘struct’. In a structure, all its members can be accessed at any time.The following creates a derived data type struct Student.

struct Student {

intstudent_id;

char student_name[20];

};

For the above structure, variables can be declared as follows.

struct Student student1, student2, student3;

There are two methods to access the members of the structure. That is by using the member operator (.) and structure pointer operator (->). The members can be accessed using structure_variable_name. member name. If the programmer wants to access the name of the student 2, then he can write the statement as printf(student2.student_namename);

Refer the below program with a structure.

Figure 01: C program with structures

According to the above program, Student is a structure. It contains student_id and student_name. Two variables of structure type are declared in the main program. They are called student1 and student2. The student1’s id is assigned with value 1 using the member operator as student1.student_id=1. The name “Ann” is a string. Therefore, it is copied to the student_name member using string copy function strcpy. The id and name are assigned to student2 in a similar way. Finally, those values are printed using member operator.

The amount of memory required to store a structure variable is the sum of the memory size of all members. The student_id contains 4 bytes and student_name contains 20 bytes (one byte each for a character). The total 24 bytes is the sum of memory size required by the structure.

What is Union in C?

A union is a user-defined data type in C. It helps to store different data types in the same memory location. A Book can have properties such as book_name, price etc. Instead of creating variables for each of them, a union can be used to compact all different data types into a one unit using a union. It is defined using the keyword ‘union’.The following creates a derived data union Book.

unionBook{

char name[20];

double price;

};

For the above union, variables can be declared as follows.

union Book book1, book2;

There are two methods to access the members of the union. That is by using the member operator (.) and structure pointer operator (->). The members can be accessed using union_variable_name. member name. If the programmer wants to access the name of the book1, then he can write the statement as printf(book1.name);

Refer the below program with a union.

Figure 02: C program using union

According to the above program, the Book is a union. The book1 is a variable of type union. The name and price are assigned values. In union, only one of its members can be accessed at a time and all other members will have garbage values. The value of id does not print properly but the price value prints properly.

Figure 03: Modified C program with union

According to the above program, Book is a union. The book1 and book2 are union type variables. First, the value for book1 name is assigned and it is printed. Then the value for book2 name is assigned and it is printed. All the members print correctly because one member is being used at a time.  The memory required to store a union is the memory required for the largest element of the union.  In the above program, the name variable is 20 bytes. It is larger than the price. So, the memory allocation for the union is 20 bytes.

What are the Similarities Between Structure and Union in C?

What is the Difference Between Structure and Union in C?

Structure vs Union in C

Structure is a user-defined datatype in C language that allows combining data of different types together. Union is a user-defined datatype in C language that allows combining data of different types together.
 Accessibility
In a structure, all its members can be accessed at any time. In a union, only one of its members can be accessed at a time and all other members will contain garbage values.
Memory Allocation
The memory required to store a structure variable is the summation of the memory size of all members. The memory required to store a union variable is the memory required for the largest element in the union.
 Keyword
The keyword used to define a structure is ‘struct’. The keyword used to define a union is ‘union’.

Summary – Structure vs Union in C

An array is used to store the data elements of the same type. Sometimes it is necessary to store data elements of different types in the same memory location. C programming language provides structure and union to accomplish this task. Both are user-defined data types. The memory required to store a structure variable is the summation of the memory size of all members. The memory required to store a union variable is the memory required for the largest element in the union. This is the difference between structure and union in C.

Reference:

1.Point, Tutorials. “Structures in C.”, Tutorials Point, 15 Aug. 2017. Available here 
2.Point, Tutorials. “Unions in C.” , Tutorials Point, 15 Aug. 2017. Available here