e1?(void_e2):(void_e3) so, is it legal or what?

Brett Galloway brett at wjvax.UUCP
Wed Aug 20 02:17:19 AEST 1986


In article <248 at desint.UUCP> geoff at desint.UUCP (Geoff Kuenning) writes:
>In article <745 at wjvax.wjvax.UUCP> brett at wjvax.UUCP (Brett Galloway) writes:
>
>> You are correct, but this is true of ALL uses of '?:`.  In fact, '?:` is
>> VERY useful, especially when you want to embed conditionals in a macro.
>> Using the if() {} else {} form restricts the contexts in which the macro
>> may appear.
>
>Unfortunately, Brett is incorrect here.  You can't use if/then/else to write:
>
>	for (i = up ? 0 : 99;  up ? (i < 100) : (i >= 0);  up ? i++ : i--)
>	    {
>	    /* complex loop body */
>	    }
>

I thought I said the same thing -- '?:` is useful in some cases where
if/then/else isn't.

>without duplicating code.  On the other hand, anywhere you want do
>if/then/else (or any other complex statement, such as loops and switches)
>inside a macro without restricting where the code can be used, you can
>just write:
>
>	#define abc(x)	do { \
>			/* anything you want goes here, even declarations */ \
>			} while (0)

I don't think this method is general.  For example, your macro abc() cannot be
used inside a for() construct.

-- 
-------------
Brett Galloway
{pesnta,twg,ios,qubix,turtlevax,tymix,vecpyr,certes,isi}!wjvax!brett



More information about the Comp.lang.c mailing list