Can I define a macro like:
#define ERROR_REPORT(LEVEL,SUB_LEVEL printf(
Compiler complain this line expecting a ;. Is there a way to escape the “(“?
This is intended to replace a function:
ERROR_REPORT(LEVEL,SUB_LEVEL
"error, there is an error %d\n",
error_id);
with a C built-in function
printf("error, there is an error %d\n",
error_id);
EDIT: I just want to replace one “(“, not include “)”. The reason why I ask this is that there are already some codes as posted in question, so I just want to replace one line like ERROR_REPROT(… with printf( .
I suggest you use variadic macros:
While you can put whatever you want as the replacement for a macro, you define it wrong:
The preprocessor needs the closing parenthesis, or it will cause errors in the preprocessing phase.
Also when you are using this macro you can’t do as you do:
The preprocessor will throw an error here too as there is no comma between the
SUB_LEVELargument and the string.So the answer to the question if you can replace a single left parenthesis is simply no, you can’t.