Path: utzoo!utgpu!attcan!uunet!lll-winken!lll-tis!helios.ee.lbl.gov!pasteur!ames!mailrus!cornell!uw-beaver!tektronix!reed!alexis From: alexis@reed.UUCP (Alexis Dimitriadis) Newsgroups: comp.lang.c Subject: Re: Unnecessary Macros (was Re: Unnecessary Parenthesis) Message-ID: <10701@reed.UUCP> Date: 22 Oct 88 22:35:23 GMT References: <2089@ssc-vax.UUCP> Reply-To: alexis@reed.UUCP (Alexis Dimitriadis) Organization: Reed College, Portland OR Lines: 27 Whoever claimed to NEVER write macros that do not behave just like functions (i.e., use their argument more than once, modify an argument, or reference local variables) has good reason for doing so. Still, features unique to macros are sometimes the most convenient way to get from here to there. Yes, there are pitfalls that should be scrupulously avoided. If my macro-with-arguments might not behave like a function, I make sure it does not _look_ like one: I spell its name in uppercase: #define MAX(x, y) (((x) > (y)) ? (x) : (y)) Am I naive in expecting any programmer to check the definition before using something named MAX() with a "dangerous" argument like ++i? Probably... BTW, I nevertheless feel that using a separately-defined local variable is BAD form. If I need an extra variable, I restrict its scope: #define SWAP(x,y) do{ long tmp = x; x = y; y = tmp; } while (0) You cannot do that with a function! (Of course, you cannot use the above in an expression...) Thank you for reading through my $0.02 worth; and forgive me if I just brought up something that got thrashed to death three months ago. Alexis Dimitriadis tektronix!reed!alexis