[comp.sys.amiga.tech] Defining macros

jms@tardis.Tymnet.COM (Joe Smith) (11/04/89)

In article <36899@srcsip.UUCP> carpent@SRC.Honeywell.COM (Todd Carpenter) writes:
>As long as we are on the subject of Lattice, why doesn't this macro seem to
>work?  (I'm rather new to macrodom, so this is probably a silly one) I
>have some terribly long nasty expressions that need to be squared, and
>SQR(moocow) never returns what I expect:
>#define SQR(x) (x*x)

That definition is guarenteed to produce the wrong results if the "nasty
expression" includes addition, subtraction, or division.

SQR(2+5) = (2+5*2+5) = (2+10+5) = (17) != 7*7

You need to put parenthises around every dummy argument and around the
whole thing.  

#define SQR(x) ((x)*(x))

-- 
Joe Smith (408)922-6220 | SMTP: JMS@F74.TYMNET.COM or jms@gemini.tymnet.com
McDonnell Douglas FSCO  | UUCP: ...!{ames,pyramid}!oliveb!tymix!tardis!jms
PO Box 49019, MS-D21    | PDP-10 support: My car's license plate is "POPJ P,"
San Jose, CA 95161-9019 | narrator.device: "I didn't say that, my Amiga did!"