🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #3175

macors and semicolons

#3175
From: andrew
Date: Mon, 24 Jun 1991 10:11
36 lines
1203 bytes
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@otago.ac.nz
Technical Details
Message-ID: <1991Jun24.213932.595@otago.ac.nz>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!mips!pacbell.com!decwrl!waikato.ac.nz!comp.vuw.ac.nz!canterbury!otago.ac.nz!andrew

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!mips!pacbell.com!decwrl!waikato.ac.nz!comp.vuw.ac.nz!canterbury!otago.ac.nz!andrew
From: andrew@otago.ac.nz
Newsgroups: comp.std.c
Subject: macors and semicolons
Message-ID: <1991Jun24.213932.595@otago.ac.nz>
Date: 24 Jun 91 10:11:08 GMT
Organization: University of Otago, Dunedin, New Zealand
Lines: 36