Skip to content

Instantly share code, notes, and snippets.

@waltervargas
Created May 13, 2021 13:10
Show Gist options
  • Save waltervargas/6c90a244cb06e68f4a401b9c6c0c8b01 to your computer and use it in GitHub Desktop.
Save waltervargas/6c90a244cb06e68f4a401b9c6c0c8b01 to your computer and use it in GitHub Desktop.
Type Generic Macros in C

Type Generic Macros in C

source

#define sin(X) _Generic((X), \
   float: sinf, \
   double: sin, \
   long double: sinl \
)(X)

int main(void) {
  float f = sin(1.5708f);
  double d = sin(3.14159);
}

result

int main(void) {
  float f = sinf(1.5708f);
  double d = sin(3.14159);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment