Type-independent ABS

Morris M. Keesan keesan at bbn.com
Sat Oct 14 06:59:48 AEST 1989


In article <1392 at cipc1.Dayton.NCR.COM> gmaranca at cipc1.Dayton.NCR.COM (Gabriel Maranca) writes:
>#define ABS(x) (((x) < 0) ? (-(x)) : (x))
. . .
>bill davidsen (davidsen at crdos1.crd.GE.COM) writes:
>>	1. instead of -(x) use (-(x)).
>Good point. I had overlooked this potential trap in my original macro.

I don't see why the extra parentheses are necessary, or their absence a trap.
#define ABS(x) ((x) < 0 ? -(x) : (x))
should work fine, because both < and - have higher precedence than ?:, and the
whole expansion is protected by the outermost parentheses.  It's not as if
-(x) were the full expansion of the macro.
------------------------------------------------------------------------------
Morris M. Keesan                    |
Internet: keesan at bbn.com            |
UUCP: Figure it out from the path   |



More information about the Comp.lang.c mailing list