Understanding Unions in C: An Easy, Fun, and Free Tutorial

Hello there, fellow coder! Today, we’re going to dive into a fascinating topic in C programming: Unions. If you’ve been scratching your head trying to understand what unions are and how they work, you’ve come to the right place. Let’s get started!

What are Unions in C?

In C, a union is a user-defined data type that allows different data types to be stored in the same memory location. Think of it as a super-efficient storage unit that can hold different types of data, but only one at a time.

How Do Unions Work in C?

Unions work by allocating enough memory to store the largest member. For instance, if a union has a char, int, and float type, it will allocate memory equivalent to the size of float, as it’s the largest data type.

union Data {
   int i;
   float f;
   char str[20];
} data;

In the above example, the size of union Data will be equivalent to the size of str, which is the largest member.

Accessing Union Members

Accessing union members is a piece of cake! You can access them just like you would access a structure member, using the dot operator.

data.i = 10;
data.f = 220.5;
strcpy( data.str, "C Programming");

printf( "data.i : %d\n", data.i);
printf( "data.f : %f\n", data.f);
printf( "data.str : %s\n", data.str);

Full executable program:

#include <stdio.h>
#include <string.h>  // Include this for strcpy

union Data {
   int i;
   float f;
   char str[20];
} data;

int main() {
    data.i = 10;
    printf( "data.i : %d\n", data.i);

    data.f = 220.5;
    printf( "data.f : %f\n", data.f);

    strcpy(data.str, "C Programming");
    printf( "data.str : %s\n", data.str);

    return 0;
}

Code Examples

Let’s look at a couple of complete C codes with explanations and outputs.

Code Example 1

#include <stdio.h>

union Job {
   float salary;
   int workerNo;
} j;

int main() {
   j.salary = 12.3;

   // when j.workerNo is assigned a value,
   // j.salary will no longer hold the previous value
   j.workerNo = 100; 

   printf("Salary = %.1f\n", j.salary);
   printf("Number of workers = %d", j.workerNo);

   return 0;
}

Code Example 2

#include <stdio.h>

union test {
   int x, y;
};

int main() {
   union test t;

   t.x = 2; // t.y also gets value 2
   printf("After making x = 2:\n x = %d, y = %d\n\n", t.x, t.y);

   t.y = 10; // t.x is also updated to 10
   printf("After making y = 10:\n x = %d, y = %d\n\n", t.x, t.y);

   return 0;
}

Wrapping Up

Unions in C are a powerful tool that allows efficient use of memory by letting different data types share the same memory location. They’re especially useful when you want to save space in your structures.

Frequently Asked Questions

How does a union work in C?

A union works by allocating enough memory to store the largest member.

What is a union in C with an example?

A union is a user-defined data type that allows different data types to be stored in the same memory location. For example, union Data { int i; float f; char str[20]; } data;

How are union members accessed in C?

Union members can be accessed just like structure members, using the dot operator.

What are the advantages of union in C?

The main advantage of unions is memory efficiency. They allow different types of data to be stored in the same memory location, saving space.

What is the difference between a structure and a union?

The main difference is that while a structure allocates separate memory for each member, a union uses the same memory for all its members.

Can a union contain structures?

Yes, a union can contain structures and vice versa.

What is the size of a union?

The size of a union in C is equal to the size of its largest member.

Can we use pointers in unions?

Yes, pointers can be used in unions.

What is the purpose of anonymous unions?

Anonymous unions are used when we want to use only one member from the union at any time without using the union variable.

Can we access all members of a union at once?

No, we can only access one member of a union at a time.

  1. Structures in C
  2. C Structures vs Unions

That’s all for now, folks! I hope this guide helped you understand unions in C a bit better. Remember, practice makes perfect. So, keep coding, keep exploring, and most importantly, have fun!

Scroll to Top