> ## 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.

# C Typedef Union

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:

```c theme={"dark"}
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:

```c theme={"dark"}
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:

```c theme={"dark"}
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**

```c theme={"dark"}
#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;
}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
