C Unions

Intermediate
6 min read
Updated: Nov 2026
1 example

What Is Unions?

A union declares several members that share the same memory. Its size is the size of the largest member, and only the most recently written member holds a meaningful value. Use unions with a tag field (tagged union) to save space when only one of several representations is active.

Remember
union Number {
    int    i;
    double d;
};
union Number n;
n.i = 42;      /* now n.d is not meaningful */

Cheat Sheet

Keep these facts handy while you work with Unions in C.

TopicDetail
SizeLargest member (plus padding)
Active memberThe last one written
TagStore which member is valid
Reading another memberReinterprets the bytes

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Structures vs Unions.

Example 1 — A tagged union

The kind field records which member is valid.

C
#include <stdio.h>

struct Number {
    enum { AS_INT, AS_DOUBLE } kind;
    union {
        int i;
        double d;
    } value;
};

static void show(struct Number n) {
    if (n.kind == AS_INT) printf("int: %d\n", n.value.i);
    else                  printf("double: %.1f\n", n.value.d);
}

int main(void) {
    struct Number a = {AS_INT, {.i = 42}};
    struct Number b = {AS_DOUBLE, {.d = 3.5}};

    show(a);
    show(b);
    printf("sizeof value = %zu\n", sizeof a.value);
    return 0;
}

How It Works

  1. kind says whether to read i or d.
  2. The union occupies 8 bytes (the size of double), not 12.
  3. Designated initializers pick the member to set.

Edge Cases & Pitfalls

Check these before relying on Unions in real programs.

read

Reading the wrong member

Without a tag you cannot tell which member is valid. Reading the wrong one gives meaningless data.

endian

Type punning is not portable

Viewing an int as bytes depends on endianness and padding.

Key Takeaways

  • Share: one block, many views.
  • Tag: track the active member.
  • Size: largest member.

One line: A union declares several members that share the same memory.

Frequently Asked Questions

A union is a type whose members all occupy the same memory location, so it holds one value at a time and has the size of its largest member.
Use unions to save memory with mutually exclusive alternatives, such as variant records or token values, and always pair them with a tag.
Continue with "C Structures vs Unions" next. Compare memory use and behavior of structs and unions. The C Structures, Unions and Enums hub lists every topic in order.

Next: Structures vs Unions

Compare memory use and behavior of structs and unions.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful