Skip to content

Instantly share code, notes, and snippets.

@RodrigoDornelles
Created June 25, 2021 13:39
Show Gist options
  • Save RodrigoDornelles/11c1618d4a05e480926103b7b2af46d9 to your computer and use it in GitHub Desktop.
Save RodrigoDornelles/11c1618d4a05e480926103b7b2af46d9 to your computer and use it in GitHub Desktop.
Simple generic programming example using C11
#include <stdio.h>
const char* tni();
const char* tnf();
#define typename(T) ((_Generic((T), int: tni, float: tnf))())
int main()
{
int foo;
float bar;
printf("variable 'foo' has the type of '%s'\n", typename(foo));
printf("variable 'bar' has the type of '%s'\n", typename(bar));
}
const char* tni()
{
static const char* type = "int";
return type;
}
const char* tnf()
{
static const char* type = "float";
return type;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment