C

C Tutorial — Learn C Programming

Key takeaway: This free C tutorial teaches you systems programming from scratch — variables, pointers, arrays, functions, structs, and memory management — with practical code examples you can compile and run.

C is a general-purpose, procedural programming language created in 1972. It is used for operating systems, embedded systems, game engines, and performance-critical applications. This tutorial is for beginners, CS students, and developers who want to understand how computers work at a low level.

Last updated: September 2026

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 foundational systems programming language that's been around since the 1970s. It's the base for Linux, Windows, and most operating systems. Learning C gives you deep understanding of memory, pointers, and how computers work — skills that transfer to every other language. According to the TIOBE Index, C consistently ranks in the top 2 most popular languages.

No, zero experience needed. Our C programming tutorial is designed for complete beginners. We start with Hello World and gradually progress to pointers and memory management. Basic computer literacy and logical thinking are helpful but not required.

Just a C compiler — or use our free online compiler. For beginners, our online C compiler requires no installation. For local development, use GCC (available on all platforms). IDEs like VS Code, Code::Blocks, or Dev-C++ also work well.

2–4 weeks for basics, 3–6 months for proficiency. With 1–2 hours daily practice, you can learn syntax, loops, and functions in a few weeks. Mastering pointers, memory management, and data structures takes 6–12 months. Practice with our C programming examples.

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.

Yes, extremely relevant. C consistently ranks in the top 2 on TIOBE and powers Linux, embedded systems, IoT, automotive software, and game engines. Understanding C makes you a better programmer in any language since most runtimes (Python, Java, Node.js) are themselves written in C.