liveBook · Manning

Assignment compatibility

As explained in Modern C

Assignment compatibility is the C property that a value can be assigned to an object of a specified type under the language's permitted conversions.

Consider checking whether a macro argument X may be used as a void*: ((void*){ 0 } = (X)) The compound literal creates a temporary void* object, and assignment applies only permitted implicit conversions. Unlike a cast, this preserves compiler type checking. TAKEAWAY: assignment compatibility asks whether X can be assigned to an object of type T. section 16.2

Checking a pointer macro argument

A pointer-tracing macro can use ((void*){ 0 } = (X)) before passing the result to the %p format. A pointer value is accepted through the permitted conversion to void*; a non-pointer argument produces a compiler diagnostic. The earlier cast-based form, (void*)(X), can suppress these checks.

The same idea supports other macro checks:

  • Use (X)+0 to require an arithmetic argument, covering integer and floating-point values. section 16.2
  • Prefer assignment over a cast when the goal is compile-time validation rather than forcibly asserting a type. section 16.2

How Modern C covers it

How deep it goes
Explains the definition, contrasts casts with assignment, and demonstrates compound-literal checking.
Examples
Demonstrates arithmetic tracing with +0 and pointer tracing with void* casts or compound-literal assignment.
What you can do after
Apply it via C compound literals to make the compiler check whether an expression is assignable to a chosen type.
How it connects
Connects assignment compatibility with implicit conversions, casts, compound literals, pointers, and variadic-format macros.

Also explained in

Compare how 3 books explain assignment compatibility →

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.