1_Generic(3) Library Functions Manual _Generic(3)
2
3
4
6 _Generic - type-generic selection
7
9 _Generic(expression, type1: e1, ... /*, default: e */);
10
12 _Generic() evaluates the path of code under the type selector that is
13 compatible with the type of the controlling expression, or default: if
14 no type is compatible.
15
16 expression is not evaluated.
17
18 This is especially useful for writing type-generic macros, that will
19 behave differently depending on the type of the argument.
20
22 C11.
23
25 C11.
26
28 The following program demonstrates how to write a replacement for the
29 standard imaxabs(3) function, which being a function can't really pro‐
30 vide what it promises: seamlessly upgrading to the widest available
31 type.
32
33 #include <stdint.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36
37 #define my_imaxabs _Generic(INTMAX_C(0), \
38 long: labs, \
39 long long: llabs \
40 /* long long long: lllabs */ \
41 )
42
43 int
44 main(void)
45 {
46 off_t a;
47
48 a = -42;
49 printf("imaxabs(%jd) == %jd\n", (intmax_t) a, my_imaxabs(a));
50 printf("&imaxabs == %p\n", &my_imaxabs);
51 printf("&labs == %p\n", &labs);
52 printf("&llabs == %p\n", &llabs);
53
54 exit(EXIT_SUCCESS);
55 }
56
57
58
59Linux man-pages 6.05 2023-05-03 _Generic(3)