1 /* Macro using defined with a hard-coded identifier (no parentheses) */ 2 #define is_foo_defined defined /*...*/ foo 3 #undef foo 4 #if is_foo_defined 5 failure 6 #else 7 success 8 #endif 9 #define foo 10 #if is_foo_defined 11 success 12 #else 13 failure 14 #endif 15 16 /* Macro using defined with a hard-coded identifier within parentheses */ 17 #define is_foo_defined_parens defined /*...*/ ( /*...*/ foo /*...*/ ) // 18 #define foo 19 #if is_foo_defined_parens 20 success 21 #else 22 failure 23 #endif 24 #undef foo 25 #if is_foo_defined_parens 26 failure 27 #else 28 success 29 #endif 30 31 /* Macro using defined with an argument identifier (no parentheses) */ 32 #define is_defined(arg) defined /*...*/ arg 33 #define foo bar 34 #undef bar 35 #if is_defined(foo) 36 failure 37 #else 38 success 39 #endif 40 #define bar bar 41 #if is_defined(foo) 42 success 43 #else 44 failure 45 #endif 46 47 /* Macro using defined with an argument identifier within parentheses */ 48 #define is_defined_parens(arg) defined /*...*/ ( /*...*/ arg /*...*/ ) // 49 #define foo bar 50 #define bar bar 51 #if is_defined_parens(foo) 52 success 53 #else 54 failure 55 #endif 56 #undef bar 57 #if is_defined_parens(foo) 58 failure 59 #else 60 success 61 #endif 62 63 /* Multiple levels of macro resulting in defined */ 64 #define X defined A && Y 65 #define Y defined B && Z 66 #define Z defined C 67 #define A 68 #define B 69 #define C 70 #if X 71 success 72 #else 73 failure 74 #endif 75 #undef A 76 #if X 77 failure 78 #else 79 success 80 #endif 81 #define A 82 #undef B 83 #if X 84 failure 85 #else 86 success 87 #endif 88 #define B 89 #undef C 90 #if X 91 failure 92 #else 93 success 94 #endif 95