liveBook · Manning

Type expression

As explained in Modern C

A type expression is a type specification appearing as an alternative association in a _Generic expression.

A type expression is a type specification used as an alternative association in a _Generic expression. The form pairs each type with an expression selected when the controlling expression has that type. section 16.6

Use these rules when writing the alternatives:

  • Write only unqualified types; array types and function types are not valid type expressions. section 16.6
  • Use pointers to qualified types, arrays, or functions when those pointer types are the intended alternatives. section 16.6
  • Account for the controlling expression’s function-argument-like type: qualifiers drop, arrays convert to pointers, and functions convert to function pointers. section 16.6

Selecting a type-specific maximum

A MAXVAL macro uses type expressions such as bool, char, signed, unsigned, signed long, and unsigned long long, associating each with its maximum value. The alternatives describe the types that _Generic may select, while the controlling expression determines the match.

How Modern C covers it

How deep it goes
Defines syntax and restrictions, then explains qualifier removal and array and function conversions.
Examples
Demonstrates integer alternatives in MAXVAL and floating-point alternatives in a type-generic min macro.
What it assumes
Assumes familiarity with _Generic expressions and macro-based type-generic programming.
What you can do after
Build type-generic macros by matching normalized expression types with appropriate alternatives.
How it connects
Connects _Generic with controlling expressions, type qualifiers, array-to-pointer conversion, and function-to-pointer conversion.

Also explained in

Compare how 2 books explain type expression →

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.