liveBook · Manning

Compound literal

As explained in Modern C

A compound literal is a C expression consisting of a parenthesized type name followed by an initializer that defines an unnamed object.

A typed, unnamed object

```c unsigned value = ((unsigned){ 7 }); ``` The syntax is a parenthesized type name followed by an initializer: `(T){ INIT }`. The expression creates an unnamed object of type `T`; it is therefore not a constant in C’s narrow sense. **TAKEAWAY:** A compound literal defines an object. section 5.6.4

Compound literals are useful when a macro needs a value with a chosen aggregate type. For example, a macro can expand to a `const` array initialized with designated elements, allowing indexed use without a separately declared array. Leave a space between the macro name and the literal’s `()`; otherwise the preprocessor can read it as a function-like macro definition. section 5.6.4

   (T){ INIT }

Use this compact syntax pattern to recognize the type-and-initializer structure. section 5.6.4

Several rules distinguish compound literals from ordinary constants and declarations:

  • An automatic compound literal’s initializer is evaluated each time execution reaches its definition. section 13.3
  • For an object that is not a variable-length array, its lifetime begins when its scope is entered and ends when that scope is left. section 13.3
  • A compound literal can enforce assignment compatibility in a macro: `((T){ 0 } = (X))` assigns the argument to a temporary object of type `T`, enabling compiler checking. section 16.2
  • Do not hide a terminating semicolon inside a macro definition; the replacement is text, and the caller should supply the statement terminator. section 5.6.4

Lifetime and address behavior

```c void fgoto(unsigned n) { unsigned j = 0; unsigned *p = 0, *q; AGAIN: if (p) printf("%u: %s, *p is %u\n", j, q == p ? "equal" : "unequal", *p); q = p; p = &((unsigned){ j, }); ++j; if (j <= n) goto AGAIN; } ``` Each visit evaluates the initializer again. The demonstrated output shows successive values `0` and `1`; when the same literal is revisited through the jump, its address relationship is defined by the object’s scope and lifetime. section 13.3

How Modern C covers it

How deep it goes
Defines syntax, object semantics, macro pitfalls, lifetime, repeated evaluation, and assignment compatibility without broader aggregate internals.
Examples
Uses a named corvid array macro, a `goto` lifetime experiment, and pointer-tracing macros for concrete demonstrations.
What you can do after
Apply compound literals in C macros and diagnose their constant-expression, lifetime, and type-checking consequences.
How it connects
Connects compound literals to macros, designated initializers, object lifetime, variable-length arrays, and assignment compatibility.

Also explained in

Compare how 2 books explain compound literal →

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.