Created
June 25, 2021 13:39
-
-
Save RodrigoDornelles/11c1618d4a05e480926103b7b2af46d9 to your computer and use it in GitHub Desktop.
Simple generic programming example using C11
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> | |
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