Tutorial by Examples

#include <stdio.h> #define is_const_int(x) _Generic((&x), \ const int *: "a const int", \ int *: "a non-const int", \ default: "of other type") int main(void) { const int i = 1; int j = 1; double...
#include <stdio.h> void print_int(int x) { printf("int: %d\n", x); } void print_dbl(double x) { printf("double: %g\n", x); } void print_default() { puts("unknown argument"); } #define print(X) _Generic((X), \ int: print_int, \ double: pri...
If a selection on multiple arguments for a type generic expression is wanted, and all types in question are arithmetic types, an easy way to avoid nested _Generic expressions is to use addition of the parameters in the controlling expression: int max_int(int, int); unsigned max_unsigned(unsigned, ...

Page 1 of 1