macors and semicolons

andrew at otago.ac.nz andrew at otago.ac.nz
Mon Jun 24 20:11:08 AEST 1991


I often get pissed off with the C pre-processor.  Here is one thats been
getting up my wick for months.

#define SWAP(a, b) {int c;  c = a; a = b; b = c}

if (spam)
   SWAP(a, b);
else
   a++;

These lines of code do a simple substitution

if (spam)
   {
   int c;
   c = a;
   a = b;
   b = c
   };
else
  a++;

notice the }; on the last line of the substitution.  This means that the else
is a syntax error!  With swap, there are a number of simple solutions...

#define SWAP(a, b) (b = (b ^ a) ^ (a = (b ^ a) ^ a))

simple hu?  but it only words with ints or longs. 

Question time....
how do I define a swap macro that swaps two doubles, allows the ';' on the end
of the macro call, but does not cause a systax error when used in this context?


Andrew
andrew at otago.ac.nz



More information about the Comp.std.c mailing list