Union Basics in C

Let’s say you are creating a program to record the name and quantity of different goods, where quantity might be count, weight or volume. One way to approach the problem is to create structure as follows:

As we know balls quantity is measured using count. So, in this case, there is no need for weight and volume.

Similarly in the following statement:

As the quantity of flour is measured using weight. So, in this case, there is no need to store count and volume.

From these observations, we can conclude that, a particular type of goods at a time can be measured using only one of the quantity either a count or a weight or a volume.

At this point our program has following limitations:

  • It takes more space than required, hence less efficient.
  • Someone might set more than one value.

It would be much more useful if we could record quantity using either a count, a weight, or a volume. That way we can save a lot of memory.

In C, a union allows us to do just that.

What is a Union ?

Like structures, unions are used to create new data types. It can also contain members just like structures. The syntax of defining a union, creating union variables and accessing members of the union is same as that of structures, the only difference is that union keyword is used instead of structure .

The important difference between structures and unions is that in structures each member has it’s own memory whereas members in unions share the same memory. When a variable of type union is declared the compiler allocates memory sufficient to hold the largest member of the union. Since all members share the same memory you can only use one member of a union at a time, thus union is used to save memory. The syntax of declaring a union is as follows:

Syntax:

Just like structure you can declare union variable with union definition or separately.

If we have a union variable then we can access members of union using dot operator (.) , similarly if we have pointer to union then we can access members of union using arrow operator (->) .

The following program demonstrates how to use a union.

Expected Output:

How it works:

In lines 7-12, a union data is declared with three members namely var1 of type int, var2 of type double and var3 of type char. When the compiler sees the definition of union it will allocate sufficient memory to hold the largest member of the union. In this case, the largest member is double, so it will allocate 8 bytes of memory. If the above definition would have been declared as a structure, the compiler would have allocated 13 bytes (8+4+2) of memory (here we are ignoring holes, click here to learn more about it).

In line 16, a union variable t of type union data is declared.

In line 18, the first member of t i.e var1 is initialized with a value of 10. The important thing to note is that at this point the other two members contain garbage values.

In line 19, the value of t.var1 is printed using the printf() statement.

In line 21, the second member of t i.e var2 is assigned a value of 20.34. At this point, the other two members contain garbage values.

In line 22, the value of t.var2 is printed using printf() statement.

In line 24, the third member of t i.e var3 is assigned a value of 'a' . At this point, the other two members contain garbage values.

In line 25, the value of t.var3 is printed using printf() statement.

In line 27, the sizeof() operator is used to print the size of the union. Since we know that, in the case of a union, the compiler allocates sufficient memory to hold the largest member. The largest member of union data is var2 so the sizeof() operator returns 8 bytes which is then printed using the printf() statement.

Initializing Union Variable

In the above program, we have seen how we can initialize individual members of a union variable. We can also initialize the union variable at the time of declaration, but there is a limitation. Since union share same memory all the members can’t hold the values simultaneously. So we can only initialize one of the members of the union at the time of declaration and this privilege goes to the first member. For example:

This statement initializes the union variable j or in other words, it initializes only the first member of the union variable j.

Designated initializer

Designated initializer allows us to set the value of a member other than the first member of the union. Let’s say we want to initialize the var2 member of union data at the time of declaration. Here is how we can do it.

This will set the value of var2 to 9.14. Similarly, we can initialize the value of the third member at the time of declaration.

The following program demonstrates the difference between a structure and a pointer.

Expected Output:

How it works:

In lines 6-11, a structure of type s is declared with three members namely var1 of type int, var2 of type float and var3 of type char.

In line 13-18, a union of type u is declared with three members namely var1 of type int, var2 of type float and var3 of type char.

In line 22 and 23 declares a structure variable a of type struct s and union variable b of type union u respectively.

In line 27, the address of structure variable a is printed using & operator.

In line 28, the size of structure variable is printed using sizeof() operator.

Similarly the printf() statements in line 38 and 39 prints address and size of union variable b respectively.

All the member of a union shares the same memory that’s why the next three printf() statements prints the same address.

difference-between-structure-and-union

Notice that the members of the union share the same address while the members of structure don’t. The difference in size of structure and union variable also suggests that in some cases union may provide a more economical use of memory. Another important point I want to emphasise is that size of the structure may be greater than the sum of members due to the boundary alignment discussed earlier, the same thing is true for unions.

A structure can be a member one of the union. Similarly, a union can be a member of the structure.

Let’s now shift our attention back to the problem we discussed while introducing unions.

After learning about unions we know that at a time only one member of union variable will be usable, that means the union is perfect for defining quantity. So instead if storing different quantity as members of structure why not create a union of a quantity that way for any goods only one member of the union will be usable.

Instead of nesting union quantity we can define it outside the goods structure.

If we want to access the value of count we can write:

Similarly to access the value of weight we can write:

The following program demonstrates how we can use a union as a member of the structure.

Expected Output:

How it works:

In lines 7-12, a union quantity is declared with three members namely count of type int, weight of type float and volume of type float.

In lines 14-18, structure goods is declared with 2 members namely name which is an array of characters and w of type union quantity.

In line 22, structure variable g1 is declared and initialized. The important thing to note how designated initializer is used to initialize the weight member of the union. If we would have wanted to initialize the first element, we would have done it like this:

or

In line 23, structure variable g2 is declared and initialized.

In line 25 and 26, name and weight of the first goods is printed using printf() statement.

Similarly in line 28 and 29, name and weight of the second goods is printed using printf() statement.

Leave a Comment