If I use the macro:
#define AND
in the following way:
if(...)
{
...
}
elseANDif(...)
{
...
}
What output does the preprocessor produce?
Edit:
I intend to use:
#define TEST(params) if(...){...}else
the … in if(…) is a complicated expression using params
the … in {…} performs some operations & is independent of params
#define AND
TEST(x1) AND TEST(x2)
{
//the code for the final else
}
Is the AND helping here or can I do without it?
No, this isn’t going to work as you expect. And you can test what the preprocessor does by running your code through
cpp.The technical reason is that when
cppexpands macros it looks for a complete identifier token matching this macro’s name. I.e. in your case, it looks for the identifierAND. However when it parses the code it doesn’t find such an identifier. It findselseANDifwhich is quite a different identifier. It has no way to breakelseANDifinto constituents, and that’s a good thing because otherwise macros would work very badly. Imagine:Whatever that means, in real C code this would break awfully, since
NDEBUGis almost always defined in production code to be empty (google on whatNDEBUGmeans).Regarding your edit, the best advice I can give you on such matters is DON’T DO IT. (ab)Using macros like this may appear at first to make the code more readable, but in the long term it makes it much less readable, with the added peril that macros are tricky to get perfectly right and with certain combination of tokens can blow up on you badly.
So you can definitely do without the
ANDas well as without theTESTaltogether.