C Programming

Unions in C

A union is a very interesting user-defined data type in the C programming language. It is very much similar to structures in the C programming language. However, there are some differences due to which structures and unions are considered as the independent data types in the C programming language. In a straightforward way, you can consider a union as a conjunction or union of the different data types within one container. It means that a union can hold variables belonging to different data types within the very same container.

All this discussion will start to make more sense once we go through some relevant examples based on the usage of unions in the C programming language in Linux. However, before sharing those examples with you, we would like to discuss the working of unions and structures so that you can differentiate between these two data types before taking a look at the associated examples. So, let’s take a look at the affixed portion of our article.

Unions vs. Structures in C:

We all know that a structure in C is a user-defined data type that can hold multiple values. These values can correspond to different data types. The same is the case with the unions in C. Then, where do both structures and unions differ from each other? Well, the response to this question is a little more tricky. Whenever you create a structure with more than one data member, it allocates a separate memory location for each of these data members. It means that you can assign values to all the data members at once, and they will be stored at independent memory locations.

On the other hand, in the case of unions in C, whenever you create a union with more than one data member, it does not allocate separate memory locations for all the data members. Rather a unified single space is reserved for all of these data members. It means that no matter how many data members of different data types are there within a union, it can store only a single data type.

Examples of Unions in C:

Now when you know the difference between a structure and a union, it is a good time to share some examples of unions in C to visualize the gist of this concept. We have designed the following examples to share the incorrect usage of the unions in C followed by its correct usage to understand this concept well. Let us take a look at both of these examples.

Example # 1: Incorrect Usage of Unions in C

For the first example of today’s article, we have written a C program in a file named Unions.c. In this program, we have created a union that is declared with the “union” keyword followed by the name of the union, which in our case is “SampleUnion”. In the body of this union, we have three data members belonging to different data types, namely “int”, “float”, and “char[]”. After creating a union, we have our “main()” function. In this function, we have first created an object of the union, which is created by first using the “union” keyword, followed by the name of the union, i.e., “SampleUnion” in our case, and then, the name of the object which we have chosen to be “SU”. After creating this object, we have assigned values to the data members of our union by using this object. Finally, we have printed the values of all these data members one by one.

Compilation of the C Program:

You can easily compile the C program created above, with the command shown below:

$ gcc Unions.c –o Unions

An errorless compilation of your C code will imply that you can head on to the execution of your code.

Execution of the C Program:

To execute your C program in Linux, you will have to access its object file created above in the following manner:

$ ./Unions

The output of our C program created above is shown below. However, you can clearly notice from this output that the values of the “int” and “float” data members of our union got corrupted since they got overwritten by our “char[]” data member. This means that we have incorrectly used our union by assigning different values to the data members of the union at the same time, which a union is not capable of processing.

Example # 2: Correct Usage of Unions in C

Now, let us take a look at the corrected version of our union code that we created above. This version is shown in the below-affixed image. The only difference is that this time we are printing the values of all the data members one by one, i.e., right after assigning values to them to get their actual values in the output instead of the overwritten corrupted values.

The compilation and execution methods of the C program in Linux have already been shared with you in the first example. Therefore, this time when your C program will be executed, you will be able to get the correct output values for all the data members, as shown in the image below. This time, we have correctly used the unions in C by assigning a single value to a data member at a time.

Conclusion:

Hopefully, this article provides a good insight into the concept of unions in C. By studying the different examples that we shared today, you will efficiently work with unions in C correctly.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.