fork download
  1. #include <stdio.h>
  2. #include <stdint.h>
  3.  
  4. void FooBar_U32(uint32_t x);
  5. void FooBar_DBL(double x);
  6.  
  7. #define FooBar(x) _Generic((x), uint32_t: FooBar_U32,\
  8.   double: FooBar_DBL)(x)
  9.  
  10. int main(void)
  11. {
  12. uint16_t foo = 232; // Variable foo is 16-bit unsigned
  13. FooBar(foo); // Try to promote a 16-bit to 32-bit value for FooBar_U32
  14.  
  15. float nib = 3.14;
  16. FooBar(nib); // Try to promote a float to a double for FooBar_DBL
  17. }
  18.  
  19. void FooBar_U32(uint32_t x)
  20. {
  21. printf("unsigned 32\n");
  22. }
  23.  
  24. void FooBar_DBL(double x)
  25. {
  26. printf("Double\n");
  27. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c: In function ‘main’:
prog.c:7:28: error: ‘_Generic’ selector of type ‘short unsigned int’ is not compatible with any association
 #define FooBar(x) _Generic((x), uint32_t: FooBar_U32,\
                            ^
prog.c:13:5: note: in expansion of macro ‘FooBar’
     FooBar(foo);         // Try to promote a 16-bit to 32-bit value for FooBar_U32
     ^~~~~~
prog.c:7:28: error: ‘_Generic’ selector of type ‘float’ is not compatible with any association
 #define FooBar(x) _Generic((x), uint32_t: FooBar_U32,\
                            ^
prog.c:16:5: note: in expansion of macro ‘FooBar’
     FooBar(nib);  // Try to promote a float to a double for FooBar_DBL
     ^~~~~~
stdout
Standard output is empty