All about a union in C, you should know

The C language provides the flexibility to the programmer to create the own data types as per their requirement. These data type is called user define data type or non-primitive data type. The c standard provides the tags like struct, union or enum to create the user-defined data type.

In this article, I will discuss the union and its features (including the C99 & C11) and the requirements of the union in c language.

What is a union in C?

A union is used to store the different data types in the same memory location. In union, we can define many members as per the requirement but here need to remember that stored value is shared by all members. It means union provides the efficient way of using the same memory location but carefully.


union [name of union] {member-list };

Parameters of union
The type name was given to the union.
Members that the union can contain.

Note: A union type declaration is a template only. There is no memory reserved for the union until a variable is declared.

Defining a union in C

Similar to the structure we can define a union. At the time of declaration of union, union tag is optional and each member of union define like a normal variable. The number of member variable depends on requirement.

In C language, at the end of the union’s (close braces), before the final semicolon, we can define a union variable but it is optional and depend on the developer.

In below union, data is union variable and Age, fees, name are member variables.

Now using the data (union variable), we can store an integer, a floating-point number or a string of characters and each member share the same memory location.

Accessing union Members using variable and pointer

Union members access by a dot ( .) or arrow ( -> ) operator, the left (first) operand of the operator should be variable of union or pointer to the union and right (second) operand shall name of a member that you want to access.

Let’s take an example to understand the above expression.

In the above example, There is a three-member variable of union price, ProductId, and name. I have created a Laptop1 a variable of union and a Laptop2 pointer to the union.

Now, suppose you want to access the ProductId using Laptop1 (union variable ), so it can be accessed as:

Laptop1. ProductId

Likewise, if you want to access the ProductId using Laptop2 ( pointer to a union), so it can be accessed as:

Laptop2-> ProductId

You can also write the above expression using the. operator.

(*Laptop2). ProductId


Program example to understand how to access members of the union.


At the beginning of the article, I have explained that union members share the same memory location. So if a union have two or more than two types of member and assigned the value to any one member, but the accessed the value through other type members, the results will be unreliable.

Let see an example to understand the above concept.
We have declared a union of float and int. If we assigned a float value to the float member, but later tried accesses the value as an int. In such a situation, the value would depend on the internal storage of float values. The integer value would not be reliable.

Output: unreliable

Note: So in a union, we have to access one member at a time.

How to calculate the size of a union?

The members of a union share the same memory location, according to c standard “size of a union is sufficient to contain the largest of its members” (plus required padding). It means union follows the alignment as per the largest member.

Let see the below example,

Output: If the size of the int is 4 bytes, the size of the union will be 12 bytes (due to extra padding bytes).

Output: If the size of the double is 8 bytes, the size of the union will be 16 bytes (due to extra padding bytes).

If you want to learn more about the c language, here 10 Free days (up to 200 minutes) C video course for you.

Your free trial is waiting


Application of union in C

Many developers do not use the union properly, they do not take the advantage of the union. Basically, the main purpose of a union is to save the memory because union members share the same memory location.

A union is a work like a washroom, different people use it in a different time frame (non-overlapping time period) but nobody uses it at the same time. So a single washroom handle the many people and there is no need to create washroom for each person, it saves your money.

Like that, if in the application there are a lot of objects that hold the value at a non-overlapping time, you can create the union for these object to save the memory. Just like a washroom has at most one “active” users at each moment of time, a union has at most one “active” member at each moment of program time.

This allows the size of struct InfoData to be only 8 bytes, instead of 24.

To access the register in a more convenient way we can put bit-field structure and integral data type in a union, which enable the way to access the entire the register or individual bits.

See the below example,

Suppose a microcontroller has a port of 8 pins and each pin are connected to the led. In that scenario using the bit field, we can easily change the status of the led.

So first we need to create a bit-field structure to mapping with the micro-controller port.

Create a pointer to the above describe bit-field and assign the address of the PORT to the pointer which you want to access.

volatile LED_BAR_STATE *pLedState = (volatile LED_BAR_STATE *)0xE002C000;

Now you can access the individual led using the pointer.

pLedState->LED1 = 1;

pLedState->LED2 = 0;

Note: Here, I am only describing, how is the bit-field work. I am not suggesting to use bit-field in the mapping of a hardware register because the allocation of bit-field depends upon the compiler.

Might be the result of one compiler can be different from another compiler. So we should avoid the compiler-dependent code, In simple word, avoid using bit fields for the mapping of the hardware register.

Some important points of a union in C

  • A union has the declaration-list define a new type within the translation unit.
  • According to the c standard, If the struct-declaration-list does not contain any named members, either directly or via an anonymous structure or anonymous union, the
    behavior is undefined.

Note: GCC permits a C structure to have no members.

struct empty {
The structure has size zero.

  • A union or struct with no tag is called an anonymous union or structure. An anonymous structure or union is introduced in C11 not supported by the c99 or older compiler.

struct //anonymous struct
short int b;

union //anonymous union
short int b;

  • If a structure contains the anonymous structure or union(only in C11), The members of an anonymous structure or union are considered to be members of the containing structure or union.

  • The above describe rule applies recursively if the containing structure or union is also anonymous.

  • We can create bit-field using the union.

  • Each non-bit-field member of a structure or union object is aligned in an implementation-defined manner appropriate to its type.
  • The size of a union is sufficient to contain the largest of its members.
  • A pointer to a union object, suitably converted, points to each of its members (or if a member is a bitfield, then to the unit in which it resides), and vice versa.
  • Designated initialization of union is also supported by C99 and C11.

Difference between structure and union

The key difference between structure and union is that structure allocate enough space to store all the fields but unions only allocate enough space to store the largest field. In union, all fields are stored in the same space.

In below table, I have listed some common difference between structure and union.

difference between union and structure

Leave a Reply