This one bit me today

Tom Karzes karzes at mfci.UUCP
Sun Oct 8 01:26:19 AEST 1989


In article <1067 at polari.UUCP> 6sigma at .UUCP (Brian Matthews) writes:
>That's why anyone who knows what they're doing will parenthesize each
>argument in the expansion of the macro and use liberal whitespace, thusly:
>
>#define xavg(m)	(sum + 3) / (m)
>
>Then things like xavg (*p) and xavg (x + y) work as expected.

It's also why people who *really* know what they're doing parenthesize
both the arguments and the top level of their expression macros, thusly:

#define xavg(m)         ((sum + 3) / (m))

Otherwise if you write something like:

    a / xavg(b)

your buggy macro would give you:

    a / (sum + 3) / (b)

rather than:

    a / ((sum + 3) / (b))

Which, as you can see, has an entirely different meaning.



More information about the Comp.lang.c mailing list