macro inefficiency

Peter Klausler pmk at craycos.com
Mon Oct 1 10:22:17 AEST 1990


In article <5062 at quiche.cs.mcgill.ca> utility at quiche.cs.mcgill.ca (Ronald BODKIN) writes:
>In article <2514 at cirrusl.UUCP> dhesi%cirrusl at oliveb.ATC.olivetti.com (Rahul Dhesi) writes:
>[suggesting a swap macro with typeof:]
>>I have seen a "typeof" keyword proposed that would fix this deficieny:
>>
>>#define swap(a,b)  do { typeof(a) tmp; tmp = a;  a = b; b = tmp; } until (0);
>	This should just be defined as:
>#define swap(a,b)  { typeof(a) tmp; tmp = a;  a = b; b = tmp; }
>
>(check out your C grammar carefully and you will see that
>{ declaration* statement* } is always a valid statement).

Check out your C grammar even more carefully and you will see that

	#define swap (a,b) { typeof (a) tmp; tmp = a; a = b; b = tmp; }

		if (...)
			swap (a,b);
		else
			...

is erroneous, while

	#define swap (a,b) do { typeof (a) tmp; tmp = a; a = b; b = tmp; } \
				while (0)

		if (...)
			swap (a,b);
		else
			...

works.

(Note that it's "do {...} while (...)", not "do {...} until (...);".)

-Peter Klausler, Cray Computer Corp. compiler development



More information about the Comp.lang.c mailing list