Actions

EmSys

CCoding Style - Avoid function macros

From EdWiki

Revision as of 09:30, 23 June 2015 by Jshankar (Talk | contribs) (1 revision imported: EdWiki -- Emsys pages from Shukra)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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) )