Generic selection
suggest changeC11 introduced the _Generic primary expression, which selects an expression according to the type of a controlling expression:
#define type_name(value) _Generic((value), \
int: "int", \
double: "double", \
default: "other")
puts(type_name(42)); /* Selects "int". */
The controlling expression is not evaluated. Its type is matched against the association list, and exactly one compatible association is selected. No two associations may specify compatible types, and at most one default association is allowed.
Conversions applied to the controlling expression can affect matching. In particular, array and function expressions commonly undergo their usual pointer conversions, and lvalue conversion removes top-level const, volatile, and restrict qualification. Add deliberate casts or take an address when a macro needs to distinguish cases that these conversions would otherwise erase.
Every result expression must still be semantically valid after macro expansion, even when it is not selected. _Generic is therefore useful for type-directed interfaces, but it cannot always hide code that is invalid for the other listed types.