difference between union and structure in c

Difference Between Union and Structure in C: An In-Depth Overview

C programming language provides two unique data structures known as union and structure. Both structures permit holding and accessing a collection of related data items, but they are implemented differently and carry distinct purposes in C programming.

What is a Structure in C Programming Language?

In C, a structure is a composite data type that holds a collection of items of any data type, and these items can be of different data types. Structures provide a way to group different types of data items into a single entity, which can be further utilized by accessing individual elements of the structure using a dot (.) operator.

For instance, consider the example below, where a structure is defined with three elements – name, age, and roll number.

“`
struct Student
{
char name[20];
int age;
int rollno;
};
“`

This structure can hold information for a single student, including their name, age, and roll number. The elements of a structure can be accessed individually by accessing their name as shown below:

“`
struct Student s1;
s1.age = 25;
printf(“Age of %s is %d”,s1.name,s1.age);
“`

What is a Union in C Programming Language?

In contrast to a structure, a union is a data structure that permits holding various data types in one memory location. This memory location is shared between all members of the union, and only one value can be stored and accessed at a time.

See also  difference between sip and mutual fund

For instance, consider the below example where a union is defined with three elements- integer, float, and char.

“`
union test
{
int a;
float b;
char c;
};
“`

In the above example, we can see an instance of a union that can hold an integer, a floating-point number, or a character. A union is typically utilized when memory optimization is crucial as it saves memory by sharing a structure’s space for various purposes.

The Difference between Union and Structure in C Programming Language

The primary difference between a structure and a union is their memory allocation. In a structure, all the members have separate memory space, and each member can store and access its own value. In contrast, union members share a single memory space, and only one member can store and access a value at a given time.

Another difference between the two is that in a structure, all elements are stored in the order they are defined, while in a union, elements share the same memory, and the size of a union is the largest element’s size.

Conclusion

In conclusion, both structure and union cater to storing and accessing information in C programming language. The significant difference between the two is their memory allocation and the way they access data. A structure is used when different data types need to be grouped together for better readability, whereas a union is utilized when memory optimization is crucial.

See also  15+ Examples of Minimalist Fish Pond Designs

Table difference between union and structure in c

Union Structure
A union is a user-defined data type that enables you to combine multiple variables that share the same memory location. A structure is a user-defined data type that enables you to combine multiple variables of different data types together under one name.
A union can only hold one value at a time, and the size of the union is determined by the size of its largest member. A structure can hold multiple values at a time, and the size of the structure is determined by the sum of the sizes of its members.
When you modify one member of a union, the value of all other members is automatically changed. When you modify one member of a structure, the value of other members is not affected.
A union is useful when you need to save memory in the data structure. A structure is useful when you need to store related data together and access it easily.