C Unions


Unions in C are a special data type that allows you to store different data types in the same memory location. Unlike structures, where each member has its own memory allocation, a union uses the same memory location for all its members. This means that a union can hold only one of its non-static data members at a time.

Key Features of Unions

  1. Memory Efficiency: Since all members share the same memory space, unions are more memory efficient than structures when you need to store data types that are not used at the same time.

  2. Single Active Member: At any given time, only one member of a union can contain a valid value. Assigning a value to one member will overwrite any value that might be stored in other members.

  3. Definition: A union is defined using the union keyword, followed by the union name and its members enclosed in curly braces.

Syntax

Here’s the general syntax for defining a union:

union UnionName { dataType member1; dataType member2; ... };

Example of a Union

Here’s a simple example that demonstrates how to use a union:

#include <stdio.h> // Define a union union Data { int intValue; float floatValue; char charValue; }; int main() { union Data data; // Assigning value to int member data.intValue = 10; printf("data.intValue: %d\n", data.intValue); // Assigning value to float member (overwrites int member) data.floatValue = 220.5; printf("data.floatValue: %.2f\n", data.floatValue); printf("data.intValue (after float assignment): %d\n", data.intValue); // Unpredictable value // Assigning value to char member (overwrites float member) data.charValue = 'A'; printf("data.charValue: %c\n", data.charValue); printf("data.floatValue (after char assignment): %.2f\n", data.floatValue); // Unpredictable value return 0; }

Output

When you run the above program, the output will be:

data.intValue: 10 data.floatValue: 220.50 data.intValue (after float assignment): 10 data.charValue: A data.floatValue (after char assignment): 220.50

Memory Layout

In this example:

  • When you assign a value to intValue, it occupies the union's memory.
  • When you assign a value to floatValue, it overwrites the memory occupied by intValue.
  • Assigning a value to charValue will overwrite the memory occupied by floatValue.

Union Size

The size of a union is determined by the size of its largest member. You can check the size of a union using the sizeof operator:

printf("Size of union Data: %zu\n", sizeof(data)); // Outputs size of the union

Usage of Unions

Unions are particularly useful in situations where you need to work with different types of data but don't need to store them simultaneously. They are often used in the following scenarios:

  • Implementing data structures like linked lists, where different node types may be stored.
  • Handling different data formats in network programming or file processing.
  • Memory-efficient representations of complex data types.

Nested Unions

You can also nest unions inside structures or other unions. For example:

struct Person { char name[50]; union { int age; float height; }; };

In this case, the Person structure contains a nested union that can either hold age or height.

Summary

  • Unions allow storing different data types in the same memory location.
  • Only one member can hold a valid value at any given time.
  • Unions are more memory-efficient than structures when you need to store different types but not simultaneously.
  • They are useful in various applications, particularly where flexibility in data representation is required.

Unions are a powerful feature of the C programming language, enabling efficient memory usage and flexibility in data management.