what is the better way to use macro and why
1)
CHECK(foo());
#define CHECK(foo) do{ \
UCHAR status = foo; \
if(0 != status) \
// do some stuff \
return status; \
}while(0)
or
2)
UCHAR status = foo();
CHECK(status);
#define CHECK(status) do{ \
if(0 != status) \
// do some stuff \
return status; \
}while(0)
edited
thank You for all of You guys, a lot of people say that it is not good to use such piece of the code, but I have a lot of such pieces in my code (which I didn’t write, only modify), what can You suggest?
I’d say the first one, since it takes care of avoiding multiple evaluation of
foo, and who uses it doesn’t need to remember to create the extra variable.Still, personally I don’t like macros that alter the execution flow like that, a programmer first seeing the codebase can easily miss a return point of the function.