C Unions

A union is a user-defined type similar to structs in C programming. We recommend you to learn C structs before you check this tutorial.

How to define a union?

We use the union keyword to define unions. Here’s an example:

  1. union car
  2. {
  3. char name[50];
  4. int price;
  5. };

The above code defines a derived type union car.

Create union variables

When a union is defined, it creates a user-defined type. However, no memory is allocated. To allocate memory for a given union type and work with it, we need to create variables.

Here’s how we create union variables.

  1. union car
  2. {
  3. char name[50];
  4. int price;
  5. };
  6. int main()
  7. {
  8. union car car1, car2, *car3;
  9. return 0;
  10. }

Another way of creating union variables is:

  1. union car
  2. {
  3. char name[50];
  4. int price;
  5. } car1, car2, *car3;

In both cases, union variables car1car2, and a union pointer car3 of union car type are created.

Access members of a union

We use the . operator to access members of a union. To access pointer variables, we use also use the -> operator.

In the above example,

  • To access price for car1car1.price is used.
  • To access price using car3, either (*car3).price or car3->price can be used.

Difference between unions and structures

Let’s take an example to demonstrate the difference between unions and structures:

  1. #include <stdio.h>
  2. union unionJob
  3. {
  4. //defining a union
  5. char name[32];
  6. float salary;
  7. int workerNo;
  8. } uJob;
  9. struct structJob
  10. {
  11. char name[32];
  12. float salary;
  13. int workerNo;
  14. } sJob;
  15. int main()
  16. {
  17. printf("size of union = %d bytes", sizeof(uJob));
  18. printf("\nsize of structure = %d bytes", sizeof(sJob));
  19. return 0;
  20. }


size of union = 32
size of structure = 40

Why this difference in the size of union and structure variables?

Here, the size of sJob is 40 bytes because

  • the size of name[32] is 32 bytes
  • the size of salary is 4 bytes
  • the size of workerNo is 4 bytes

However, the size of uJob is 32 bytes. It’s because the size of a union variable will always be the size of its largest element. In the above example, the size of its largest element, (name[32]), is 32 bytes.

Only one union member can be accessed at a time

You can access all members of a structure at once as sufficient memory is allocated for all members. However, it’s not the case in unions. You can only access a single member of a union at one time. Let’s see an example.

  1. #include <stdio.h>
  2. union Job
  3. {
  4. float salary;
  5. int workerNo;
  6. } j;
  7. int main()
  8. {
  9. j.salary = 12.3;
  10. j.workerNo = 100;
  11. printf("Salary = %.1f\n", j.salary);
  12. printf("Number of workers = %d", j.workerNo);
  13. return 0;
  14. }


Salary = 0.0
Number of workers = 100

Notice that 12.3 was not stored in j.salary.

Leave a Reply

Your email address will not be published. Required fields are marked *