Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A typedef union in C is a composite declaration that defines a memory-sharing data structure and simultaneously assigns it a user-defined type alias. By combining the union keyword (which allocates a single shared memory space for multiple members) with the typedef keyword (which creates a type alias), developers can instantiate variables of this type without repeatedly using the union keyword in the declaration. Syntax and Structure The standard declaration of a typedef union follows this structure:
typedef union [optional_tag] {
    type1 member1;
    type2 member2;
    /* ... */
} AliasName;
  • optional_tag: The internal identifier for the union. It can be omitted (creating an anonymous union bound to the typedef) unless the union needs to self-reference via pointers.
  • AliasName: The new type identifier used to declare variables.
Declaration Comparison Without typedef, declaring a variable requires the union keyword in the type specifier:
union NumericData {
    int integer_val;
    double double_val;
};

union NumericData my_data; /* 'union' keyword is mandatory */
With typedef, the alias acts as a standalone type specifier:
typedef union {
    int integer_val;
    double double_val;
} NumericData;

NumericData my_data; /* 'union' keyword is omitted */
Memory Layout and Mechanics When a variable of a typedef union is instantiated, the compiler enforces strict memory overlapping rules:
  1. Shared Base Address: Every member of the union begins at the exact same memory address. The offset of every member from the beginning of the union is zero.
  2. Size Determination: The sizeof the entire typedef union is equal to the size of its largest member, plus any additional bytes required to satisfy the strict alignment requirements of the target architecture (padding).
  3. Mutual Exclusion: Because members share the same memory footprint, assigning a value to one member overwrites the underlying byte representation of all other members. Reading from a member other than the one most recently written to results in type punning, where the compiler interprets the raw bytes according to the newly accessed member’s type.
Example of Memory Mechanics
#include <stdio.h>

typedef union {
    char bytes[4];    /* Size: 4 bytes */
    int integer;      /* Size: 4 bytes */
    double floating;  /* Size: 8 bytes */
} MemoryBlock;

int main() {
    MemoryBlock block;
    
    /* The size of MemoryBlock is 8 bytes (dictated by the double member) */
    printf("Size: %zu bytes\n", sizeof(MemoryBlock));
    
    /* Writing to the integer member */
    block.integer = 0x11223344;
    
    /* 
     * The character array shares the exact same memory space as the integer.
     * Reading block.bytes[0] will yield 0x44 or 0x11 depending strictly 
     * on the endianness of the host architecture.
     */
    printf("Byte 0: 0x%X\n", block.bytes[0]);
    
    return 0;
}
Master C with Deep Grasping Methodology!Learn More