+ 1

Is it legal to use conditional preprocessors inside a macro function?

In the following program, I tried out every syntax to make the commented block to work but it just failed. Can anyone fix the second way (the commented block of code) ? https://sololearn.com/compiler-playground/ciDRHzC9n8pb/?ref=app

17th Feb 2025, 6:02 PM
Ali_combination
Ali_combination - avatar
12 odpowiedzi
+ 3
Ali_combination The short answer is no. The first way is the right way to do it. The second way cannot be fixed because C does not permit preprocessor directives to be embedded within a macro definition.
17th Feb 2025, 6:54 PM
Brian
Brian - avatar
+ 3
Ali_combination similar to your question https://stackoverflow.com/questions/154136/why-use-apparently-meaningless-do-while-and-if-else-statements-in-macros tldr: it's a way to make the semicolons in your macro behave properly when it's expanded, to avoid unexpected bugs. so maybe use it when your macro have semicolons.... here's another link with example code: https://www.geeksforgeeks.org/multiline-macros-in-c/
18th Feb 2025, 10:40 AM
Bob_Li
Bob_Li - avatar
+ 2
Brian ok then, thanks
17th Feb 2025, 7:55 PM
Ali_combination
Ali_combination - avatar
+ 1
isn't line 5 supposed to be #ifndef DBUG // ifndef instead of ifdef ? your code should raise an error because of (5<5), but it doesn't... edit: ok, # ifdef works but you have to uncomment the //#define DBUG above it. I just assumed it was a regular comment explaining the macro. I did not notice it's actually a commented out #define...
18th Feb 2025, 5:33 AM
Bob_Li
Bob_Li - avatar
+ 1
Ali_combination it would have the same behavior if you were to remove the do while(0) and leave the braces. Furthermore, even the braces are unnecessary. So it boils down to a simpler form as below. I suggest that in order to make the macro blank, make it to mimic a void return. #ifdef DBUG #define ASSERT(EXPR) assert(EXPR) #else #define ASSERT(expr) ((void) 0) #endif
18th Feb 2025, 9:55 AM
Brian
Brian - avatar
+ 1
Ali_combination it is recommended to use do while(0) to enclose multiple statements in a macro definition. Beware that such a macro can be used only where a do while() statement is valid. It cannot be used as a term within an expression.
18th Feb 2025, 10:36 AM
Brian
Brian - avatar
+ 1
Thank you all.
18th Feb 2025, 10:51 AM
Ali_combination
Ali_combination - avatar
+ 1
Vicy can you provide a working example of using preprocessor directives within a multi-line macro definition?
19th Feb 2025, 1:42 PM
Brian
Brian - avatar
0
Bob_Li if the DBUG is defined, an assert may raise. If not, all asserts are ignored.
18th Feb 2025, 6:11 AM
Ali_combination
Ali_combination - avatar
0
Brian I have another question if I may. Will there be any difference if I use do while(0) syntax or solely a brace-enclosed syntax ? Will both of them be fine ?
18th Feb 2025, 6:24 AM
Ali_combination
Ali_combination - avatar
0
Brian what about multiline macro functions? Will do-while method be the same as braces method ?
18th Feb 2025, 9:58 AM
Ali_combination
Ali_combination - avatar
0
No, it's not illegal to use conditional preprocessors inside a macro function. It's actually common for adapting code based on different conditions. For example, you can use #ifdef inside a macro to include or exclude certain code based on whether a specific flag is set. Just be cautious—overusing preprocessors can make the code harder to maintain and debug.
19th Feb 2025, 1:35 PM
Fatxi
Fatxi - avatar