Simple C Question (was: down)

Byron A Jeff byron at pyr.gatech.EDU
Mon Dec 19 09:34:22 AEST 1988


In article <4385 at Portia.Stanford.EDU> jrll at Portia.stanford.edu (john ralls) writes:
-In article <192 at broadway.UUCP> furlani at broadway.UUCP (John L. Furlani) writes:
->
->In article <gables.352 at umigw.miami.edu>, slores%gables.span at umigw.miami.edu (Stanislaw L. Olejniczak) writes:
->> PLEASE don't flame for posting too simple a question.  I think the following
->> The purpose is to count characters in a file.  Each time a newline is
->
->            if (c == '\r') chcnt++;
-
-True, this will count carriage returns and not newlines.  In fact, it
-will count only carriage returns, which isn't what he had in mind.  He
-wanted to count all characters, counting newlines as two characters (ie,
-cr/lf).  Two ways to do it:
-	if (c == '\n') chcnt +=2; else chcnt++;
-
-or for those who like the ternary operator (like me):
-
-	c == '\n' ? chcnt +=2 : chcnt++;
-
-John

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?

BAJ
-- 
Another random extraction from the mental bit stream of...
Byron A. Jeff
Georgia Tech, Atlanta GA 30332
Internet:	byron at pyr.gatech.edu  uucp:	...!gatech!pyr!byron



More information about the Comp.lang.c mailing list