Re: Difference between a MACRO and a FUNCTION



Richard Heathfield <rjh@xxxxxxxxxxxxxxx> writes:
Philip Potter said:
Richard Heathfield wrote:
Army1987 said:
On Thu, 20 Sep 2007 00:04:05 -0700, MAx wrote:

<snip>

#define MAX(a,b) (a>b) ? a:b
Please add enough parentheses:
((a) > (b) ? a : (b))

You missed some.

<snip>

Really?

No, not really - at least, I just tried three times to break his version
and failed each time. So I owe him an apology.
[...]

Nevertheless, I would certainly use parentheses myself:
#define MAX(a, b) ((a) > (b) ? (a) : (b))
rather than
#define MAX(a, b) ((a) > (b) ? a : (b))

Consistency is certainly part of it. Another part is that I'm *still*
not 100% convinced that the parentheses are unnecessary. I have a
nagging hunch that there *might* be an argument that would break the
first form, perhaps something involving the ?: operator in the operand
itself. Or perhaps there's some argument that should be a syntax
error, but that will cause the unparenthesized version to expand to a
legal expression that doesn't do what you want.

It's very likely that my concern is groundless, and that the
unparenthesized form is perfectly safe. But if it takes an
experienced C programmer like RH three tries, not to prove that it's
safe, but merely to fail to prove that it isn't, then it's likely to
waste some of the time of every attentive programmer who reads it. If
I'm tracking down a bug that manifests itself anywhere near any use of
this macro, I'm likely to waste time convincing myself that the macro
isn't the problem before persuing other possibilities.

We had something similar here recently, with a macro definition
something like:

#define N -42

Several people immediately complained that it *should* be

#define N (-42)

but it took a day or two before anyone was able to present an example
where it actually made a difference.

--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
.



Relevant Pages

  • Re: [Luxasm-devel] The "Unified Model"
    ... be done using the _current_ design of the macro system and some ... the directives -- I will address what and where momentarily. ... done in the NASM macros I am using to write Luxasm.) ...
    (alt.lang.asm)
  • Re: Insert Time Not Updated
    ... But a macro is the way to go ... Re this suggestion, Unlinking the field (Unlink Field, which makes it plain ... Sub InsertTime() ...
    (microsoft.public.mac.office.word)
  • Re: "action" in UK33496
    ... that no new OP code or macro will intrude on the space ceded to users. ... The component prefix registry could be used for this, ... I would, sort of, like HLASM to implement "namespaces". ...
    (bit.listserv.ibm-main)
  • Re: what will be the value of #define
    ... convention for ensuring that multiple inclusions of header files ... #ifndef FRED_H ... then the first inclusion will have the macro "FRED_H" undefined, ...
    (comp.lang.c)