Built with Tutify.io

    AIStudyLabs

    Introduction to Unions

    Introduction to Unions

    Introduction

    In the previous lesson, you learned how to model related data using structures. In this lesson, you will explore unions—a closely related but very different feature of C that lets multiple variables share the same memory location. You will see how unions are declared and used, how they differ from structures in terms of memory layout, and where they are useful in real-world programs.

    By the end, you should be comfortable declaring unions, accessing their members, and understanding how data type sizes affect their behavior.


    What Is a Union and Why Use It?

    A union is a user-defined type that allows different members to share the same memory. At any given time, a union can reliably hold only one of its members.

    You might use a union when:

    Conceptually:


    Declaring and Using a Union

    Basic Union Syntax

    The syntax of a union is similar to a struct:

    union UnionName {
        type1 member1;
        type2 member2;
        // ... more members
    };
    

    Simple Example: Number as Int or Float

    #include <stdio.h>
    
    union Number {
        int i;
        float f;
    };
    
    int main(void) {
        union Number n;   // declare a union variable
    
        n.i = 42;         // store an int
        printf("As int: %d\n", n.i);
    
        n.f = 3.14f;      // store a float (overwrites same memory)
        printf("As float: %.2f\n", n.f);
    
        // After writing to n.f, n.i now refers to the same bytes
        printf("Interpreted as int now: %d\n", n.i);
    
        return 0;
    }
    
    // Output
    As int: 42
    As float: 3.14
    Interpreted as int now: (some implementation-dependent value)
    

    Explanation:

    This illustrates a crucial rule: only the most recently assigned member of a union should be read.


    Memory Usage: Structures vs Unions

    You already know that structures allocate memory for all their members. Unions are different.

    Comparing Memory Layout

    #include <stdio.h>
    
    struct SData {
        int i;
        float f;
        char c;
    };
    
    union UData {
        int i;
        float f;
        char c;
    };
    
    int main(void) {
        printf("Size of struct SData: %zu bytes\n", sizeof(struct SData));
        printf("Size of union  UData: %zu bytes\n", sizeof(union UData));
    
        return 0;
    }
    
    // Possible Output (depends on system)
    Size of struct SData: 12 bytes
    Size of union  UData: 4 bytes
    

    Explanation:


    Real-World Style Example: Sensor Reading

    Imagine a sensor that can return either an integer status code or a floating-point measured value, but never both at the same time. A union models this nicely.

    #include <stdio.h>
    
    enum ReadingType {
        READING_STATUS,
        READING_VALUE
    };
    
    union SensorData {
        int status;    // e.g., error code
        float value;   // e.g., temperature
    };
    
    struct SensorReading {
        enum ReadingType type;  // tells us which union member is valid
        union SensorData data;  // union holding the actual data
    };
    
    void print_reading(struct SensorReading r) {
        if (r.type == READING_STATUS) {
            printf("Status code: %d\n", r.data.status);
        } else if (r.type == READING_VALUE) {
            printf("Sensor value: %.2f\n", r.data.value);
        }
    }
    
    int main(void) {
        struct SensorReading r1;
        r1.type = READING_STATUS;
        r1.data.status = -1;    // error code
    
        struct SensorReading r2;
        r2.type = READING_VALUE;
        r2.data.value = 23.75f; // temperature
    
        print_reading(r1);
        print_reading(r2);
    
        return 0;
    }
    
    // Output
    Status code: -1
    Sensor value: 23.75
    

    Explanation:

    This is a practical, safe way to use unions in larger programs.


    Impact of Data Types and Sizes in Unions

    Since all members share the same memory:

    1. Largest member dominates size
      If a union contains char, int, and double:

      • The union’s size will be at least sizeof(double).
    2. Alignment and padding matter
      The union will usually be aligned to suit its most strictly aligned member (e.g., double).

    3. Interpreting bytes incorrectly is dangerous
      Assign to one member, then reading another is only meaningful when you specifically intend to reinterpret the raw bytes (e.g., bit patterns, low-level I/O). Otherwise, it leads to undefined or non-portable behavior.

    4. Portability concerns
      Bit patterns for types like float and int depend on:

      • Endianness (byte order)
      • Floating-point representation
      • Compiler-specific padding/alignment

    Therefore, unions are powerful but must be used with a clear understanding of the data sizes and representation.


    Summary / Key Takeaways

    In this lesson on Introduction to Unions, you have learned:

    C Programming Fundamentals: From Zero to Confident C Coder — Structures and Unions in C

    Introduction to Programming and C

    Variables and Data Types

    Control Structures: Conditions and Loops

    Functions in C

    Arrays and Strings

    Working with Pointers

    File Handling in C

    Structures and Unions in C

    Project Development and Implementation

    Advanced C Concepts and Best Practices

    Tutorial Not Found