Helpful C Hint

J. Wong wong at rtech.UUCP
Sun Apr 17 17:39:52 AEST 1988


Here's an interesting optimization (of which I'm sure some are
already well aware :-)

Occassionally, I write a conditional as follows:

	if ( ( a && b ) || ( !a && c && d ) )
		error();

where "a" is often something like "version == 1".

I dislike doing this because the condition "a" gets evaluated twice.  
I just realized I could rewrite this using the "?:" operator to only
evaluate "a" once:

	if ( a ? ( b ) : ( c && d ) )
		error();

(Depending on your compiler, this may not be more optimal.)



More information about the Comp.lang.c mailing list