I am trying to compile the following (in MSVC):
#define TRAP (errorCode = (errorCode != 0) ? errorCode :)
int someFunction(int a) {
printf("Called function with parameter %d\", a);
return (a > 3);
}
int main(void) {
int errorCode = 0;
printf("Error code starts out as %d\n", errorCode); // errorCode should be 0
TRAP someFunction(1);
printf("Error code is now %d\n", errorCode); // errorCode should still be 0
TRAP someFunction(4);
printf("Error code is now %d\n", errorCode); // errorCode should be 1
TRAP someFunction(2);
printf("Error code is now %d\n", errorCode); // errorCode should still be 1, someFunction should not be called.
return 0;
}
However, I get a compiler error at the first line containing "TRAP"
error C2059: syntax error : ')'
My question is: why? As I understand it, the macro pre-processor just does a find-replace for all instances of "TRAP" and replaces it with the text of the macro between the outermost parentheses. Thus, after the macro does its work, I would expect the first TRAP line to read:
errorCode = (errorCode != 0) ? errorCode : someFunction(1);
which is valid C; indeed inserting this line directly into my code compiles fine. Is there some macro nuance that I'm missing here?
I apologize if this is a stupid question--I'm pretty new to macros and a bit confused.