Many built-in data types are available in the C programming language. It also gives users the ability to create their own data kinds. Bit-field, structure, union, typedef, and enumeration are the five methods for constructing bespoke data. We shall learn about structure and union in this blog. In C programming, both structure and union are user-defined data types that may hold any data type. While structure and union appear to have similar responsibilities, there are a few distinctions to be made. Let’s have a look at the distinction between structure and union.
What is Structure?
In the C programming language, a structure is a special data type. Multiple members of various data kinds can be held in a single structure. Structure elements are stored in contiguous memory regions and may be accessed and retrieved at any time. A member or field is a data object in a structure.
The struct statement is used to define a structure. The struct keyword creates a new data type that has several members.
Syntax of Declaring a Structure
struct [structure name]
{
type member_1;
type member_2;
. . .
type member_n;
};
Example of Structure
struct student
{
int rollno;
char name[57];
string phone;
};
What is Union?
A union is a data type that has been specified by the user. It’s similar to the structure, except that all of its members begin at the same memory position. The union stores a collection of items of diverse data kinds in the same memory region. A user can create a union with numerous members, but only one member can hold a value at any given moment. The total storage space required by the union’s biggest data member is equal to the storage space allotted for the union variable.
Union offers variables that may be accessed in several ways while being in the same memory address. A union is a useful approach to use a single memory region for several tasks.
A union is defined using the union statement. It introduces a new data type that allows numerous member variables of various data types to be stored in the same memory region. The syntax for using the union keyword to define union is similar to that of constructing a structure.
Syntax of Declaring a Union
union [union name]
{
type member_1;
type member_2;
. . .
type member_n;
};
Example of Union
union Student {
char name[25];
int age;
string email;
};
Structure and Union: Difference
A structure and a union are both ways of combining multiple types of information about a single item. The fundamental distinction between the two is the manner in which specific data is stored and accessible. The following table summarises the major distinctions between structure and union:
Structure | Union |
To define a structure, we utilise the struct statement | To define a union, we use the union keyword |
Every member has their own memory place | A memory location is shared by all data members |
A change in the value of one data member has no effect on the structure’s other data members | A change in one data member’s value has an impact on the value of other data members |
Multiple members can be initialised at the same time | Only the first member can be initialised at a time |
Multiple values of various members can be stored in a structure | For all of its members, a union stores one value at a time |
The overall size of a structure is the sum of the sizes of all data members | The biggest data member determines the total size of a union |
At any moment, users can access or recover any member | Only one member can be accessed or retrieved at a time |
Conclusion
We spoke about how structure and unions function and how they vary in this blog. In C programming, both structure and union are user-defined data types that hold numerous members of various data types. Unions help manage memory effectively, whereas structures are utilised when we need to store separate values for all members in the same memory region.