Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!wuarchive!zaphod.mps.ohio-state.edu!uakari.primate.wisc.edu!pikes!aspen.craycos.com!pmk From: pmk@craycos.com (Peter Klausler) Newsgroups: comp.lang.c Subject: Re: macro inefficiency Message-ID: <1990Oct1.002217.4945@craycos.com> Date: 1 Oct 90 00:22:17 GMT References: <2514@cirrusl.UUCP> <5062@quiche.cs.mcgill.ca> Organization: Cray Computer Corporation Lines: 36 In article <5062@quiche.cs.mcgill.ca> utility@quiche.cs.mcgill.ca (Ronald BODKIN) writes: >In article <2514@cirrusl.UUCP> dhesi%cirrusl@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