Unnecessary Parenthesis

Simon A Reap sar at datcon.UUCP
Sat Sep 24 01:15:36 AEST 1988


(old discussion, but our News was gebustenbroken for yonks)
In article <1401 at devsys.oakhill.UUCP> steve at oakhill.UUCP (steve) writes three
alternatives for the square(x) macro:

>>   #define square(x)    (x * x)
>>   #define square(x)    ((x) * (x))
>>   #define square(x)    (temp = (x),(temp * temp))
>
Please note, the third version is *dangerous*.  An expression such as:

    z = square(x) + square(y);

expands to:

    z = (temp = (x),(temp * temp)) + (temp = (y),(temp * temp));

which looks OK, but note that, although C guarantees to perform the
assignment (temp = (x)) before the multiplication (temp * temp) in each
expansion of square(x), but it does *not* guarantee to complete the
first expansion before it starts the second (or vice versa).  I have
seen at least 2 compilers which do the first assignment, then the second
assignment (to the same 'temp' variable), followed by the 2 multi-
plications, so you end up with the same value of 'temp' squared twice.

Does anyone know how to get round this problem?  Please!!
(it's been bugging us for months :-( )

-- 
Enjoy,
yerluvinunclesimon                Opinions are mine - I don't even have a cat
Reach me at sar at datcon.co.uk, or ...!mcvax!ukc!pyrltd!datcon!sar



More information about the Comp.lang.c mailing list