For example, never define a macro like this:
#define DANGER 60 + 2
This can potentially be dangerous when we do an operation like this:
int wrong_value = DANGER * 2; // Expecting 124
Instead, define like this because you don’t know how the user of the macro may use it:
#define HARMLESS (60 + 2)
The example is trivial, but that pretty much explains my question. Are there any set of guidelines or best practices that you would recommend when writing a macro?
Thanks for your time!
Not only should you put parens around the arguments, you should put parens around the expression returned.
However,
MIN(3,i++)is still broken…The best rule is only to use #defines only when NO OTHER APPROACH WILL WORK! I know you’re asking about C instead of C++, but still bear his in mind.