c Union

What is a Union?

A union in C is a user-defined data type that allows you to store different types of data in the same memory location. Unlike a structure, where each member has its own memory allocation, all members of a union share the same memory space. This means that at any given time, a union can hold a value of only one of its members.

Structure vs. Union

Structure:

• Each member has its own memory location.

• The total size of the structure is the sum of the sizes of its members (considering padding and alignment).

#include <stdio.h>

struct abc {
    int a;
    char b;
};

int main() {
    struct abc example;
    printf("Size of structure: %lu\\n", sizeof(example));
    printf("Address of a: %p\\n", (void*)&example.a);
    printf("Address of b: %p\\n", (void*)&example.b);

    return 0;
}

Union:

• All members share the same memory location.

• The size of the union is determined by the size of its largest member.

#include <stdio.h>

union def {
    int a;
    char b;
};

int main() {
    union def example;
    printf("Size of union: %lu\\n", sizeof(example));
    printf("Address of a: %p\\n", (void*)&example.a);
    printf("Address of b: %p\\n", (void*)&example.b);

    return 0;
}

Why Do We Need C Unions?

Unions are particularly useful when you need to work with different data types in the same memory location. They are often used in situations where:

• Memory efficiency is important, and you only need to store one value at a time.

• You want to interpret the same memory location in multiple ways (e.g., interpreting data received from hardware or networking protocols).

Example Scenario:

Imagine you are designing a system for a store that sells both books and shirts. Each item type has specific attributes, but they also share some common attributes like price. Using a union, you can design a system to store and process different types of items efficiently.

Example:

#include <stdio.h>
#include <string.h>

union Item {
    struct {
        char title[50];
        char author[50];
        int pages;
        float price;
    } book;

    struct {
        char color[20];
        char design[20];
        char size[10];
        float price;
    } shirt;
};

int main() {
    union Item item;

    // Storing book information
    strcpy(item.book.title, "C Programming");
    strcpy(item.book.author, "Dennis Ritchie");
    item.book.pages = 200;
    item.book.price = 29.99;

    // Display book information
    printf("Book Title: %s\\n", item.book.title);
    printf("Author: %s\\n", item.book.author);
    printf("Pages: %d\\n", item.book.pages);
    printf("Price: %.2f\\n", item.book.price);

    // Storing shirt information (overwrites book information)
    strcpy(item.shirt.color, "Red");
    strcpy(item.shirt.design, "Graphic");
    strcpy(item.shirt.size, "M");
    item.shirt.price = 19.99;

    // Display shirt information
    printf("\\nShirt Color: %s\\n", item.shirt.color);
    printf("Design: %s\\n", item.shirt.design);
    printf("Size: %s\\n", item.shirt.size);
    printf("Price: %.2f\\n", item.shirt.price);

    return 0;
}

Output:

Book Title: C Programming
Author: Dennis Ritchie
Pages: 200
Price: 29.99

Shirt Color: Red
Design: Graphic
Size: M
Price: 19.99