macros and semicolons

Mark Hall markhall at pyrps5.pyramid.com
Thu Jun 27 06:23:02 AEST 1991


In article <1991Jun24.213932.595 at otago.ac.nz> andrew at otago.ac.nz writes:
>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}

Forgive me for this unsufferable horn-tooting; I just couldn't resist
(I also couldn't believe no one else sent this reply :-). . . .

NO macro will work for a swap.  You suffer from the call-by-name rule
which undid ALGOL in this case.  Consider the expansion of SWAP(i,a[i]):

	int c; c = i; i = a[i]; a[i] = c;

-Mark Hall (smart mailer): markhall at pyrps5.pyramid.com
	   (uucp paths): {ames|decwrl|sun|seismo}!pyramid!markhall



More information about the Comp.lang.c mailing list