When the preprocessor encounters TEST_MACRO(12); in main(), it performs text substitution. It replaces TEST_MACRO(12); with the macro's body, substituting x with 12. The result passed to the next stage of the preprocessor is effectively:
c
// ... inside main() ...
#if (12 == 12)
printf("x: %d\n",12);
#endif
; // The semicolon from the original macro call remains
// ... rest of main() ...
Now, the preprocessor encounters #if (12 == 12). This is a valid preprocessor conditional statement at the start of a line. 12 == 12 is true, so the code between #if and #endif is kept. The resulting code passed to the compiler is:
c
// ... inside main() ...
printf("x: %d\n",12);
; // Still the leftover semicolon
// ... rest of main() ...
The compiler sees printf("x: %d\n",12);;, which is perfectly valid (an extra semicolon is fine).
Why This Approach Works (But Isn't Standard or Recommended)
Your code can work with many compilers because the preprocessor works in multiple phases. The macro expansion happens first, and then the resulting text (which now contains #if at the start of a line) is processed for preprocessor directives.
However, this is generally not considered a standard or good practice for several reasons:
Syntactically Confusing: It mixes macro definition syntax (#define) with preprocessor directive syntax (#if, #endif) within the macro body itself. It looks like the #if is part of the definition when it's actually just text that gets substituted.
Fragile: The preprocessor rules about #if needing to be at the start of a line (ignoring whitespace) are based on processing the source file, not necessarily the intermediate text during complex macro expansion. While it often works for simple cases like this, it can lead to unexpected behavior or break in more complex scenarios or with different compilers.
Limited: You can't easily integrate this conditional code into arbitrary expressions or parts of statements. It essentially replaces the macro call line(s) with a conditional block of statements.
Standard Ways to Achieve Conditional Logic in C Macros
There are two main ways to handle conditional logic within or around macros, depending on whether you want the condition checked at compile-time (preprocessor) or runtime:
Compile-Time/Preprocessor Conditional (#if, #ifdef, etc.)
This is used to conditionally include or exclude code based on preprocessor symbols or constant expressions known at compile time.
Method 1: Checking a Macro/Symbol outside the call. This is the conventional use of #if. You'd define a symbol and then use #if around the code that uses the macro. This doesn't check the argument value like your example, but rather a global setting.
c
#define DEBUG_PRINT_ENABLED 1
#ifdef DEBUG_PRINT_ENABLED
#define TEST_MACRO(x) printf("Debug: x = %d\n", x)
#else
#define TEST_MACRO(x) ((void)0) // Define to do nothing
#endif
void main()
{
TEST_MACRO(10); // Will print if DEBUG_PRINT_ENABLED is defined
TEST_MACRO(20); // Will print if DEBUG_PRINT_ENABLED is defined
return;
}
This checks a global setting, not the value of x.
Method 2: Using preprocessor checks after expansion (like your attempt, but still not recommended for the reasons above). Your original code falls into this category. While it works, it's not the standard way.
Runtime Conditional (if)
This is the standard C way to execute code conditionally based on a condition that is evaluated while the program is running. You can easily put a standard C if statement inside a macro. The best practice for multi-statement macros (or macros containing control flow) is to wrap them in a do { ... } while(0) block so they can be used safely with semicolons and in places like after if or else.
c
#define TEST_MACRO_RUNTIME(x) \
do { \
if ((x) == 12) { \
printf("x: %d\n", x); \
} \
} while(0)
// Note: Use parentheses around x in the condition for safety
void main()
{
TEST_MACRO_RUNTIME(10); // Condition (10 == 12) is false at runtime, nothing happens
TEST_MACRO_RUNTIME(12); // Condition (12 == 12) is true at runtime, printf executes
return;
}
Conclusion
While your original code might work due to how the preprocessor handles expansion and subsequent directive processing, it's not a standard or robust way to define macros with conditional inclusion.
The best way to achieve conditional execution based on the value of a macro argument is to use a standard C if statement within a do { ... } while(0) block, as shown in the TEST_MACRO_RUNTIME example. This performs the check at runtime, which is typically what you want when examining the value of a variable or argument.
This TEST_MACRO_RUNTIME macro is the standard and recommended way to put conditional logic based on the macro's argument value inside a macro in C. The condition (x) == 12 is checked every time the macro is called at runtime.