Created
September 6, 2015 18:53
-
-
Save binki/884d7ad9b887f3304252 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
/* | |
* GCC spits out warnings that I think it shouldn’t as the standard | |
* says that “None of the expressions from any other generic | |
* association of the generic selection is evaluated.” (9899:2011 | |
* 6.5.1.1) | |
*/ | |
/* #define p(x) _Generic(x, long: printf("%ld\n", x), int: printf("%d\n", x), const char *: printf("%s\n", x), char *: printf("%s\n", x))*/ | |
/* | |
* But, anyway, it can be written more simply, readably, and | |
* without making GCC mad by just generifying over the format | |
* parameter itself. | |
*/ | |
#define p(x) printf(_Generic(x, int: "%d\n", long: "%ld\n", char *: "%s\n"), x) | |
int main(int argc, const char *const argv[]) | |
{ | |
p("hi"); | |
p(23); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
~ $ gcc -Wall -o c_generic_expression c_generic_expression.c | |
~ $ ./c_generic_expression | |
hi | |
23 | |
~ $ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment