fork(2) download
  1. #include <stdio.h>
  2.  
  3. // Here is the master list of things
  4. #define THING_DEFINITIONS \
  5.   THING(apple, true) \
  6.   THING(pear, true) \
  7.   THING(caddilac, false) \
  8.   THING(mango, true)
  9.  
  10. // I want a table of all thing names -- this works fine
  11. #undef THING
  12. #define THING(name, is_fruit) #name,
  13. char *all_things[] = {
  14. THING_DEFINITIONS
  15. };
  16.  
  17. // Now I want a table of just fruits -- the following does not work
  18. #undef THING
  19.  
  20.  
  21. // Options
  22. #define THING_true(name) #name,
  23. #define THING_false(name)
  24.  
  25. // Combine symbols
  26. #define CONCAT(a, b) a ## b
  27.  
  28. // Final macro
  29. #define THING(name, is_fruit) CONCAT(THING_, is_fruit) (name)
  30.  
  31. char *just_fruits[] = {
  32. THING_DEFINITIONS
  33. };
  34.  
  35.  
  36. int main(void) {
  37. for(int i=0;i<(sizeof just_fruits / sizeof *just_fruits);++i) {
  38. printf("%s\n", just_fruits[i]);
  39. }
  40. return 0;
  41. }
  42.  
Success #stdin #stdout 0s 9432KB
stdin
Standard input is empty
stdout
apple
pear
mango