logo of SA Coder
        Menu
sign in

      References




C programming language offers several features that allow developers to create custom data types. One such feature is unions. In this post, we will explore what unions are, how they work, and how they can be used to create custom data types.


What is a Union in C?



A union is a user-defined data type in C that allows the storage of different data types in the same memory location. The syntax for defining a union is similar to that of a structure, but there are some key differences. For instance, while structures allocate memory for all their members simultaneously, unions allocate memory only for the largest member. Therefore, a union can hold only one member at a time, and accessing another member overwrites the current one.



Defining a Union in C




To define a union, we use the keyword `union` followed by the name of the union and its members. Here's an example:


copy

union student
{
    int roll;
    int id_number;
    float marks;
};



In this example, we have defined a union named `student` with three members: `roll`, `id_number`, and `marks`. The union will allocate memory for the largest member, which in this case is the `float` data type. Therefore, the size of the union will be 4 bytes.


Using a Union in C



Once a union is defined, we can create variables of that type, just like any other data type. Here's an example:


copy

union student student_data;



This creates a variable named `student_data` of type union `student`. We can then access its members using the dot operator:


copy

student_data.roll = 5;
printf("%d\n",student_data.roll);



In this example, we have assigned the value 5 to the roll member of the student_data union and printed it to the console.


Limitations of Unions in C



While unions can be useful in some scenarios, they have certain limitations that need to be taken into account. For instance, as we mentioned earlier, a union can hold only one member at a time. Accessing another member overwrites the current one, leading to data loss. To avoid data loss, it's important to keep track of which member is currently in use. One common way to do this is to define a separate member that stores the type of the currently active member. Here's an example:


copy

union student
{
    int type;
    int roll;
    int id_number;
    float marks;
};



In this example, we have added a member named `type` that stores the currently active member. When we access a different member, we first set the `type` member to the corresponding value:


copy

student_data.type = 1; // set type to id_number
student_data.id_number = 111;



This way, we can keep track of which member is currently in use and avoid data loss.



Conclusion


Unions in C are a powerful feature that allows developers to create custom data types. By storing different data types in the same memory location, unions can save memory and simplify code. However, it's important to keep in mind their limitations and avoid data loss by keeping track of which member is currently in use.




Please login first to comment.