CCoding Style - Avoid function macros
Revision as of 06:51, 7 March 2012 by Jshankar (1 revision)
Avoid function macros
One of the most serious problems with function macros is that a parameter that appears more than once in the definition might be evaluated more than once.
If the argument in the call includes an expression with side effects, the result is a subtle bug.
#define isupper(c) ( (c) >= ‘A’ && (c) <= ‘Z’ )
Note that the parameter c occurs twice in the body of the macro. If isupper is called in a context like this,
while( isupper( c = getchar() ) )
Then each time an input character is greater than or equal to ‘A’ it will be discarded and another character read to be tested against ‘Z’.
while( ( c = getchar() ) != EOF && isupper(c) )