C

C Programming

Master the fundamentals of C programming with comprehensive tutorials, examples, and hands-on exercises

Unions in C

Unions are similar to structures but all members share the same memory location. Only one member can hold a value at any given time, making unions memory-efficient for certain applications.

Union Declaration and Usage

Basic Union Example
#include <stdio.h>

union Data {
    int intValue;
    float floatValue;
    char charValue;
};

int main() {
    union Data data;
    
    // Assign integer value
    data.intValue = 42;
    printf("Integer value: %d\n", data.intValue);
    printf("Size of union: %lu bytes\n", sizeof(data));
    
    // Assign float value (overwrites integer)
    data.floatValue = 3.14;
    printf("Float value: %.2f\n", data.floatValue);
    printf("Integer value now: %d (corrupted)\n", data.intValue);
    
    // Assign character value (overwrites float)
    data.charValue = 'A';
    printf("Character value: %c\n", data.charValue);
    printf("Float value now: %.2f (corrupted)\n", data.floatValue);
    
    return 0;
}

Union vs Structure Memory Layout

Memory Comparison
#include <stdio.h>

struct StructExample {
    int intValue;
    float floatValue;
    char charValue;
};

union UnionExample {
    int intValue;
    float floatValue;
    char charValue;
};

int main() {
    struct StructExample s;
    union UnionExample u;
    
    printf("Structure size: %lu bytes\n", sizeof(s));
    printf("Union size: %lu bytes\n", sizeof(u));
    
    printf("\nStructure member addresses:\n");
    printf("intValue: %p\n", &s.intValue);
    printf("floatValue: %p\n", &s.floatValue);
    printf("charValue: %p\n", &s.charValue);
    
    printf("\nUnion member addresses:\n");
    printf("intValue: %p\n", &u.intValue);
    printf("floatValue: %p\n", &u.floatValue);
    printf("charValue: %p\n", &u.charValue);
    
    return 0;
}

Practical Union Example

Type-Safe Union with Tag
#include <stdio.h>

enum DataType {
    TYPE_INT,
    TYPE_FLOAT,
    TYPE_CHAR
};

struct TaggedData {
    enum DataType type;
    union {
        int intValue;
        float floatValue;
        char charValue;
    } data;
};

void printData(struct TaggedData *td) {
    switch (td->type) {
        case TYPE_INT:
            printf("Integer: %d\n", td->data.intValue);
            break;
        case TYPE_FLOAT:
            printf("Float: %.2f\n", td->data.floatValue);
            break;
        case TYPE_CHAR:
            printf("Character: %c\n", td->data.charValue);
            break;
    }
}

int main() {
    struct TaggedData data1, data2, data3;
    
    // Store integer
    data1.type = TYPE_INT;
    data1.data.intValue = 100;
    
    // Store float
    data2.type = TYPE_FLOAT;
    data2.data.floatValue = 25.75;
    
    // Store character
    data3.type = TYPE_CHAR;
    data3.data.charValue = 'X';
    
    printf("Tagged union data:\n");
    printData(&data1);
    printData(&data2);
    printData(&data3);
    
    return 0;
}
Important Notes:
  • Only one member of a union can be used at a time
  • Union size equals the size of its largest member
  • All members share the same memory address
  • Use tagged unions for type safety

Frequently Asked Questions

C is a powerful, general-purpose programming language that's been around since the 1970s. It's the foundation for many modern languages and is essential for system programming, embedded systems, and understanding how computers work at a low level. Learning C gives you a solid foundation in programming fundamentals.

No prior programming experience is required! Our C programming tutorial is designed for complete beginners. We start with the basics and gradually progress to more advanced topics. However, having some basic computer literacy and logical thinking skills will be helpful.

You need a C compiler to run C programs. For beginners, we recommend using our online C compiler which requires no installation. For local development, you can use GCC (GNU Compiler Collection) which is available on Windows (via MinGW), macOS (via Xcode), and Linux. IDEs like Code::Blocks, Dev-C++, or Visual Studio Code are also helpful.

The time to learn C depends on your dedication and prior experience. With consistent practice (1-2 hours daily), you can grasp the basics in 2-4 weeks and become proficient in 2-3 months. Mastering advanced concepts like pointers, memory management, and data structures may take 6-12 months of regular practice.

C programming skills open doors to various career paths including system programming, embedded systems development, operating system development, device driver programming, game development, and firmware development. Many companies in automotive, aerospace, telecommunications, and IoT sectors actively seek C programmers.

Absolutely! C remains highly relevant and is consistently ranked among the top programming languages. It's essential for system programming, embedded systems, IoT devices, and performance-critical applications. Many modern languages are built on C, and understanding C helps you become a better programmer overall.