| by Arround The Web | No comments

Unions in C

“Just like structures, Unions are also user-defined datatypes, but unlike structures, Union members share the same memory location, and they do not have different memory locations. If we declare two variables in a Structure and then check their address, we will see that both have different addresses, which means that members of a structure get different memory locations. Instead of that, we declare the same two variables in a Union and check their addresses; it will be observed that both of their addresses are the same, which proves that both of the constants share the same memory location. In the case of the structure, an integer will take four blocks, but in a union, both the integer and character will share the same memory block.

One thing to keep in mind is that in a Union, members will share the same location of memory, which means that if changes are made in one member, they will be reflected in the other members of the union as well. The union’s size is taken according to the size of the greatest member of a union. The union members can be accessed through pointers by using the (->) operator. If we want to store information about multiple objects like Books and Stores, we will have to make two structures for each of them. It will waste a lot of memory. For that purpose, unions are used. Unions can save information to multiple objects.”

Syntax

The syntax to declare a Union is as follows:

The syntax for Unions and structure is almost the same. The difference between union and structure is that we use the keyword Union instead of struct to declare unions. In the variables section, we can see that we have declared multiple variables of different data types. This is the same as structures, but in structures, those variables can be related to a single object only, like Book or a Student or a Class, etc. But in Unions, we can declare variables relevant to more than a single object. For example, we can declare the length of a table and also the title of the book on the table.

Example 1

In this example, we have created a Union and given it the name “Test_Union”. In the variables block, we have declared two variables as members of our union. Integer “x” and character “y”. With the closing bracket, there is a variable of type union “Test_Union.” We can also declare a variable of union Type the same as in structures. With the help of “variable1”, we can access the members of our union. Inside the main() function, by the use of the dot operator, we can access the variables declared in the union. Here we are assigning the value “75” to “x.” Note that we have not assigned any value to our “y” variable.

#include <stdio.h>

union Test_Union

{

    int x;

    char y;

} variable1;

int main()

{

    variable1.x = 75;

    printf("x = %d\n",variable1.x);

    printf("y = %c\n",variable1.y);

    return 0;

}

After our code is executed, we will get the output that is shown in the figure below. We printed “x” and its value in the same way; we printed “y” and its value against it. The thing that is to be notified is that we did not assign any value to y. In our output, we can see that the compiler has printed “x” and a value of “75” against it. But against “y,” it has printed “K.” You might be wondering why we have got this capital “K” as output for “y.” Value “75” for “x” is not an issue. But “y” is a character that the compiler printed its value on even when we passed a number to “x.”

This is because, as we discussed earlier, the same memory block is shared by the members that are grouped in a union. We declared “variable1” for our union and passed the value using that variable. Variable1 assigned the value 75 to all the members of the union. Int took that value as a number and printed the same value. But the character took that value as ASCII code and converted that ASCII code into the character against it. ASCII code for capital “K” is “75,” so it replaced “75” with “K.” This is the whole process of union allocating the same memory block to the variables in it.

Example 2

In this instance, we will find out how the Union utilizes memory. For that purpose, we have declared a union as Example_Union. In Example_Union, we have declared four constants and each with different datatypes. We have declared int var1, char var2, double var3, and float var4 as the members of our Union. In the main method, we have printed a statement with the function sizeof() in it. Function sizeof() is a default function in C Language that gets the size of any object in our code. We have passed it the name of our union because we want to get the size our Union has occupied.

#include <stdio.h>

union Example_Union

{

    int var1;

    char var2;

    double var3;

    float var4;

};

int main()

{

    printf("%ld\n",sizeof(union Example_Union));

    return 0;

}

This is the output after the compilation of our code. We will get the output of 8. In my system, the size of the integer is 4 bytes, the size of the character is 1 byte, the size of a double is 8 bytes, and the size of a float is again 4 bytes. In our case, the size of a double is 8, which is the largest of all sizes, so the compiler has allocated the size 8 to our union. Now we can get a better understanding that the size of the highest variable is allocated to all the datatypes, and the sum of their sizes is not used by the compiler like in the case of structure. That is why we will get the result 8 as our output.

Conclusion

In this guide, we discussed Unions in C Language. Just to understand the concept of Unions, one can say that unions are nearly the same as structures. But the major difference is memory allocation. It is very effective when writing complex code because, at that time, a programmer focuses on the efficiency of the code. A good programmer is always trying to occupy the least memory of the system by its code; unions help them to do so. We delivered our best to help you get an understanding of unions by implementing and explaining examples. At the end of the topic, we can say that using unions is a good approach to handling multiple records as well as writing optimized and performance-tuned code. It will also help the coder to optimize the code for better performance.

Share Button

Source: linuxhint.com

Leave a Reply