A define problem

Tim McDaniel mcdaniel at adi.com
Thu Jan 10 01:19:08 AEST 1991


I suspect that the program Siva Chelliah actually compiled had
   #define half(x)  x /2
without the paren- ^ ^ theses.  In that case, half(i-5) is 3 when i==5.

Rule of thumb: PARENTHESIZE THE RESULT OF A MACRO, AND PARENTHESIZE
THE USE OF EACH ARGUMENT.

Even
   #define half(x) (x)/2
fails, if someone uses it in an expression with an operator with
higher precedence than /, as in
   ~half(i)
This Silly Little Macro should be written as
   #define half(x) ((x)/2)

--
Tim McDaniel                 Applied Dynamics Int'l.; Ann Arbor, Michigan, USA
Work phone: +1 313 973 1300                        Home phone: +1 313 677 4386
Internet: mcdaniel at adi.com                UUCP: {uunet,sharkey}!amara!mcdaniel



More information about the Comp.lang.c mailing list