liveBook · Manning

Union type

As explained in Modern C

A union is a C type whose members share the same memory location, so different members overlay one another.

A union gives several members the same memory location. Unlike a structure, it does not collect separately stored values; it overlays different type interpretations on one object representation. TAKEAWAY: A union changes how one stored object is viewed, not how many independent objects are stored. section 12.2

2   #include <inttypes.h>
3
4   typedef union unsignedInspect unsignedInspect;
5   union unsignedInspect {
6     unsigned val;
7     unsigned char bytes[sizeof(unsigned)];
8   };
9   unsignedInspect twofold = { .val = 0xAABBCCDD, };


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":1}],[{\"line\":0,\"ch\":4},{\"line\":0,\"ch\":12}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":1}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":1}],[{\"line\":2,\"ch\":4},{\"line\":2,\"ch\":17}],[{\"line\":3,\"ch\":0},{\"line\":3,\"ch\":1}],[{\"line\":2,\"ch\":12},{\"line\":2,\"ch\":17}],[{\"line\":3,\"ch\":4},{\"line\":3,\"ch\":9}],[{\"line\":4,\"ch\":0},{\"line\":4,\"ch\":1}],[{\"line\":2,\"ch\":18},{\"line\":2,\"ch\":26}],[{\"line\":3,\"ch\":10},{\"line\":3,\"ch\":18}],[{\"line\":4,\"ch\":6},{\"line\":4,\"ch\":14}],[{\"line\":5,\"ch\":6},{\"line\":5,\"ch\":14}],[{\"line\":7,\"ch\":4},{\"line\":7,\"ch\":12}],[{\"line\":5,\"ch\":0},{\"line\":5,\"ch\":1}],[{\"line\":5,\"ch\":6},{\"line\":5,\"ch\":19}],[{\"line\":5,\"ch\":26},{\"line\":5,\"ch\":32}],[{\"line\":2,\"ch\":18},{\"line\":2,\"ch\":26}],[{\"line\":3,\"ch\":10},{\"line\":3,\"ch\":18}],[{\"line\":4,\"ch\":6},{\"line\":4,\"ch\":14}],[{\"line\":5,\"ch\":6},{\"line\":5,\"ch\":14}],[{\"line\":7,\"ch\":4},{\"line\":7,\"ch\":12}],[{\"line\":6,\"ch\":0},{\"line\":6,\"ch\":1}],[{\"line\":7,\"ch\":0},{\"line\":7,\"ch\":1}]]"}
!@%STYLE%@!

Inspect the complete declaration: an unsigned value and an unsigned-char array occupy the same union object. section 12.2

Inspecting an unsigned value

Declare `unsignedInspect twofold = { .val = 0xAABBCCDD };`, then print `twofold.val` and each element of `twofold.bytes`. The value prints as `0xAABBCCDD`; on the demonstrated machine, the bytes print `0xDD`, `0xCC`, `0xBB`, and `0xAA`. The array exposes the same storage as individual unsigned-char representation bytes. TAKEAWAY: The observed byte order belongs to that implementation, not to a universal C layout rule. section 12.2

Keep these distinctions in view:

  • A union overlays members; a structure stores its members as separate parts of a larger object. section 12.2
  • An object can be examined as an array of unsigned char, but byte order and representation details can depend on the implementation. section 12.8
  • Unions are introduced after the memory model because their useful interpretation requires understanding object representation. section 6.0

How Modern C covers it

How deep it goes
Defines overlay semantics and works through byte output, while deferring broader alignment and representation limits.
Examples
Demonstrates an unsigned value overlaid with an unsigned-char array and prints its bytes in machine-dependent order.
What it assumes
Assumes basic derived types and teaches unions after introducing object representation, storage, and memory alignment.
What you can do after
Apply it via C union declarations to inspect an unsigned object's representation and compare observed byte order.
How it connects
Connects unions with structures, arrays, object representation, memory alignment, and the C memory model.

Also explained in

Compare how 10 books explain union type →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C. The section links point to the parts of the book it draws on.