Skip to content

Instantly share code, notes, and snippets.

@binki
Created September 6, 2015 18:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save binki/884d7ad9b887f3304252 to your computer and use it in GitHub Desktop.
Save binki/884d7ad9b887f3304252 to your computer and use it in GitHub Desktop.
#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;
}
~ $ 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