Simple C Question (was: down)

Mark A Terribile mat at mole-end.UUCP
Wed Dec 21 17:14:35 AEST 1988


> I think I like one of the following two better:
> chcnt += (c == '\n') ? 2 : 1;
> chcnt += 1 + (c == '\n');
> 
> Any comments one which one of the ones we've seen is most efficient?

I certainly hope that the economy or practicality of your program does not
hinge on this!  What's more important, I think, is that unless you have good
reason for using tricks (e.g. you are writing a ``compiler'' that produces C)
your second form

	... + ( c == n )

works because [ 0 1 ] is the ordered set of things you want to
AND COINCIDENTALLY [ 0 1 ] is the range of the C equality operators.

Making your code depend on such a coincidence forces the reader to be mindful
of the coincidence.  If the code is human-written to be human-read, I believe
that it's very poor practice.

Besides, any respectable compiler should generate the same code for both,
modulo the assembly language label names (if such there be).
-- 

(This man's opinions are his own.)
>From mole-end				Mark Terribile



More information about the Comp.lang.c mailing list