Search notes:

C: _Generic

Determine the type of an expression at runtime.
#define getType(x) _Generic ( (x) ,   \
   int    : "int"                 ,   \
   char   : "char"                ,   \
   char*  : "pointer to a char"   ,   \
   float  : "float"               ,   \
   double : "double"              ,   \
   void*  : "pointer to anything" ,   \
   default: "?"                       \
)

#define printType(x) printf("The type of %-15s is: %s\n", #x, getType(x))

#include <stdio.h>

int main () {
  printType (42);
  printType (42.42);
  printType (42.42f);
  printType ("Hello World");
  printType (NULL);
}
Github repository about-c, path: /_Generic.c

See also

C

Index