Generic programming: Difference between revisions

Content deleted Content added
Tags: Mobile edit Mobile web edit
Tags: Mobile edit Mobile web edit
 
Line 717:
typeof (b) _b = (b); \
_a > _b ? _a : _b; })</syntaxhighlight>
 
The keyword <code>_Generic</code> is used in C preprocessor macros to automatically match the type of its parameter.
 
<syntaxhighlight lang="c">
#include <stdio.h>
 
#define type_of(x) _Generic((x), \
int: "int", \
float: "float", \
double: "double", \
char*: "string", \
default: "unknown")
 
int main(int argc, char* argv[]) {
printf("%s\n", type_of(42));
printf("%s\n", type_of(3.14f));
printf("%s\n", type_of(2.718));
printf("%s\n", type_of("hello"));
printf("%s\n", type_of((void *)0));
return 0;
}
</syntaxhighlight>
 
==See also==