Skip to content

Instantly share code, notes, and snippets.

@nyux
Last active August 29, 2015 14:05
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 nyux/0a99a4691acf9669a898 to your computer and use it in GitHub Desktop.
Save nyux/0a99a4691acf9669a898 to your computer and use it in GitHub Desktop.
#include <stdio.h>
/* does weird behavior in clang. it ignores the char* rule and casts char*
parameters to ints. no clue why. the uncommented version has the fix, and i'm
not sure why it works.
#define abs(x) _Generic((x), \
long int: absl,\
double: absd, \
char*: abss, \
default: absi \
)(x)
*/
#define abs(x) _Generic((0, x), \
long int: absl,\
double: absd, \
char*: abss, \
default: absi \
)(x)
void absl(long x) { printf("this is a long: %ld\n", x); }
void absd(double x) { printf("this is a double: %f\n", x); }
void abss(char *x) { printf("no strings in absolute value funcs\n"); }
void absi(int x) { printf("this is an int: %d\n", x); }
int main(void)
{
abs(1L);
abs(3.14);
abs("hello");
abs(18);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment