w3resource

Understanding the Concept and use Cases of Unions in C

Introduction to Unions in C

Overview

In C programming, a union is a user-defined data type that allows multiple members to share the same memory location. Unlike a structure, where each member has its own memory space, a union saves memory by allocating space equal to the size of its largest member. Only one member of a union can be accessed at a time.

Key Topics:

Syntax of Union in C

union union_name {
    data_type1 member1;
    data_type2 member2;
    // More members can be added
};

Where -

  • union_name: The name of the union.
  • data_type: The type of each member (e.g., int, float, char[], etc.).
  • member: The names of the individual members that share the same memory space.

Example of Union Declaration:

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

When and Why to use Union

When to use Union:

  • Memory Efficiency: Use unions when memory usage is critical, such as in embedded systems or low-memory environments. Since all members of a union share the same memory space, it reduces the overall memory footprint.
  • Handling Multiple Data Types: Use unions when you need to work with different data types but don’t need to store them simultaneously. For instance, if you’re writing a program that processes various data types but only one type at a time, a union is ideal.

Why use Union:

  • Save Memory: Since all union members share the same memory location, it ensures that your program uses the smallest amount of memory possible for different types of data.
  • Efficient Data Storage: In scenarios like protocol stacks, unions are useful for defining messages with different types of data fields, as only one field needs to be active at a time.
  • Versatility: Unions allow flexibility in managing different data types. For example, they are useful in hardware programming where you need to interpret memory in different ways (e.g., as an integer or as a float).

Using unions effectively can make our program more efficient and help us manage memory more wisely in systems where resources are limited.

Let’s explore the concept and use cases of unions with examples, accompanied by explanations.

Example: Declaring and using a Union

In this example, we see how a union allows storing different types of data, but only one member holds a value at any time, as all members share the same memory space.

Code:

#include <stdio.h>
// Define a union named Data
union Data {
    int i;         // 4 bytes
    float f;       // 4 bytes
    char str[20];  // 20 bytes
};

int main() {
    union Data data;  // Declare a union variable
    // Assign an integer value
    data.i = 10;
    printf("data.i: %d\n", data.i);
    // Assign a float value (overwrites the integer)
    data.f = 220.5;
    printf("data.f: %.2f\n", data.f);

    // Assign a string (overwrites the float)
    snprintf(data.str, sizeof(data.str), "Hello");
    printf("data.str: %s\n", data.str);
    return 0;
}

Output:

data.i: 10
data.f: 220.50
data.str: Hello

Explanation:

  • We define a union Data with three members: an int, a float, and a char array.
  • When we assign values to the union members, the data gets overwritten because all members share the same memory space.
  • Initially, we store an integer, then overwrite it with a float, and finally assign a string.

Example: Accessing Union Members using the same Memory

This example highlights how assigning a value to one union member can affect other members, as they share the same memory location.

Code:

#include <stdio.h>
// Define a union for different data types
union Number {
    int i;
    float f;
};
int main() {
    union Number num;
    num.i = 1024;  // Assign an integer
    printf("Integer: %d\n", num.i);
    num.f = 3.14;  // Assign a float (overwrites the integer)
    printf("Float: %.2f\n", num.f);
    // Notice how the integer value is affected after assigning a float
    printf("After assigning float, integer: %d\n", num.i);
    return 0;
}

Output:

Integer: 1024
Float: 3.14
After assigning float, integer: 1078523331

Explanation:

  • We define a union Number with two members: an integer i and a float f.
  • After assigning an integer, we assign a float, which overwrites the memory previously occupied by the integer. This demonstrates the memory-sharing behavior of unions.

Example: Practical use case of Union – Handling multiple Data types

In this example, a union is used alongside an enum to store and track different data types, demonstrating how unions can be useful in practical scenarios like managing various types of data.

Code:

#include <stdio.h>
// Define an enum to identify the data type
enum DataType { INTEGER, FLOAT, STRING };
// Define a union to store different data types
union Data {
    int i;
    float f;
    char str[20];
};

// Define a structure to combine the union and enum
struct TypedData {
    enum DataType type;
    union Data data;
};
int main() {
    struct TypedData item;
    // Assign an integer value to the union
    item.type = INTEGER;
    item.data.i = 42;
    if (item.type == INTEGER) {
        printf("Integer: %d\n", item.data.i);
    }
    // Assign a float value to the union
    item.type = FLOAT;
    item.data.f = 3.1415;
    if (item.type == FLOAT) {
        printf("Float: %.4f\n", item.data.f);
    }
    // Assign a string value to the union
    item.type = STRING;
    snprintf(item.data.str, sizeof(item.data.str), "Union Example");
    if (item.type == STRING) {
        printf("String: %s\n", item.data.str);
    }
    return 0;
}

Output:

Integer: 42
Float: 3.1415
String: Union Example

Explanation:

  • Here, we define a union Data and combine it with an enum DataType in a struct TypedData.
  • The enum keeps track of the current data type being stored in the union.
  • This example demonstrates how unions can be used to handle multiple data types in a more organized way.


Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/c-programming/c-union.php