C – Unions

In this lesson, we will learn how to understand Unions in C. We will cover the following concepts:

  • What are Unions in C
  • Structures vs Unions
  • Create a Union with examples
  • Access Union Members with examples

What are Unions

Unions in C are a special data type that allows you to store different data types in the same memory location.

Unions are often used in situations where memory efficiency is critical, such as in embedded systems, communication protocols, and hardware interfacing.

Key concepts related to Unions:

  • Memory Sharing: Unions are useful when you need to work with different data types but only need to store one type at a time.
  • Size: The size of a union is determined by the size of its largest member.
  • Access: You can only access the value of the member last assigned. Previous values are overwritten.

Unions vs Structures

Structures in C language allow you to store data of different types, whereas Unions allow storing different datatypes but in the same memory location.

The Unions and Structures in C language are user-defined datatypes. Let us see the difference:

  • Definition
    Structures in C allow you to store data of different types. The memory for each declared variable in a Structure is allocated to each variable member.
    Unions in C allow you to store data of different types, but the memory for the declared variables in a Union is allocated to the largest memory.
  • Size
    The size of the Structure is calculated by adding the sum of its members. Therefore, the size would be greater than or equal to the sum of each member’s size.
    The size of the Union is not hard to calculate. It is the size of its largest member.
  • How to access members?
    In Structures, the individual members can be accessed at a time.
    In Union, you can access only one member at a time.
  • Keyword
    To define a Structure, use the keyword struct.
    To define a Union, use the keyword union.

Create a Union in C

Let us understand the Union concept with an example to define a union. To define a Union, use the union keyword:

Above,

  • The name of the union is Demo,
  • The union members are a, b, c, and d

Let us now see an example and see how many bytes of memory will get allocated in the below program. The largest member of the Union here is double c therefore 8 bytes of memory get allocated and the result would be 8:

Output

Above, we have used the sizeof operator in C. It is used to determine the size, in bytes, of a data type or object, Here, we are using it to get the size of union.

Now, let us modify the above example. We have changed the variable d to 20.

Now, the largest member is the char d[20], which is 20 bytes. However, for the alignment requirement of the largest type, double i.e. 8 bytes, the total size padded up to the nearest multiple of the alignment size. Therefore, the output will be 20 + 4 i.e., 24:

Output

Above,

  • The 20 bytes are for char d[20].
  • The 4 bytes of padding is added to make the total size a multiple of 8 bytes (aligning to double’s requirement).

Access Union Members

We will see how to access union members in C Language. For this purpose, use the member access operator (.). Let us see an example:

Output

In the above example, the largest member of the Union is char d[20], therefore the bytes of memory get allocated and size 24 can be seen for the largest d variable. Since it is Union, not a Structure, therefore the largest memory occupied by the variable will get considered for the memory allocation. The rest of the variable values are corrupted.

This can be fixed by working on one variable at a time as shown below. Here, demo is declared as a variable of type union Demo:

Output

In the above example, the corrupted values are now fixed. Rest, the size of the Union is the same, as expected.

If you liked the tutorial, spread the word and share the link and our website Studyopedia with others:


For Videos, Join Our YouTube Channel: Join Now


Read More:

C - Command Line Arguments
C - Variable Scope
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading