down

Richard A. O'Keefe ok at quintus.uucp
Mon Dec 12 17:41:09 AEST 1988


In article <gables.352 at umigw.miami.edu> slores%gables.span at umigw.miami.edu (Stanislaw L. Olejniczak) writes:
>	while ( (c = getchar) != EOF)
>		chcnt++ += (c == '\n');

No way is this too complex for a compiler.  It is just plain illegal.
chcnt is a 'long' _variable_ (L-value), but chcnt++ is a 'long' _number_
(R-value).  The first time round, chcnt would be 0, then chcnt++ increments
chcnt to 1 and evaluates to 0.  What would "0 += (c == '\n') mean?

Try
	chcnt++, chcnt += c=='\n';

Better still would be to use a for loop:
	for (chcnt = 0; (c = getchar()) != EOF; chcnt++) {
	    if (c == '\n') chcnt++;
	}
or
	for (chcnt = 0; (c = getchar()) != EOF; chcnt += c == '\n' ? 2 : 1)
	    ;

(Was that really (c = getchar), or was it (c = getchar())?  If the former,
that's wrong too.)



More information about the Comp.lang.c mailing list