I am trying to process some code to catch and print error messages. I am currently using a macro, for example:
switch(function(x)) { \
case ERROR: \
fprintf(stderr, "Error!\n"); \
break; \
}
Usually I never record the output of a function, and this works fine. But I found a couple of cases where I also need to return the value function(). I tried something like the following, but this leads to a syntax error.
do { \
int __err = function(x); \
switch(__err) { \
case ERROR: \
fprintf(stderr, "Error!\n"); \
break; \
} \
__err; \
} while(0)
I can declare a global variable to hold the return value of the function, but it looks ugly and my program is multi-threaded, which can cause problems. I hope there will be a better solution.
source
share