Re: strange output



In article <44F49C75.8F90E33B@xxxxxxxxx>,
CBFalconer <cbfalconer@xxxxxxxxxxxxx> wrote:

#define swap(a,b) { ..... }

Either:

#define swap(a,b) do {temp=(a); (a)=(b); (b)=temp;} while (0)

Or:

#define swap(a,b) (temp=(a),(a)=(b),(void)(b)=temp)

No. Only the do while (0) method will work in all circumstances.
With the braces you will get errors with:

if (condition) swap(x, y);

because of the terminal semicolon.

You get a problem with the braces, but not with the parenthesised
version.

I disagree.
There's nothing wrong with:

if (condition) (temp=(a),(a)=(b),(void)(b)=temp);

True.

You never follow an "if" with an "else"?

The parenthesised version, with a semicolon, is fine in that case.

The do { } version has the advantage that you can include declarations.

-- Richard

.



Relevant Pages

  • Re: c.l.c wiki update
    ... CBFalconer wrote ... >> Mainly known for it's silly indenting of the braces, ... arguing with? ...
    (comp.lang.c)
  • Re: strange output
    ... CBFalconer wrote: ... the whole macro: ... With the braces you will get errors with: ... Clark S. Cox III ...
    (comp.lang.c)
  • Re: strange output
    ... CBFalconer wrote: ... Frederick Gotham wrote: ... the whole macro: ... With the braces you will get errors with: ...
    (comp.lang.c)