(c == '\n') is 1 or 0

Walter Bright bright at Data-IO.COM
Thu Dec 15 04:59:58 AEST 1988


In article <349 at greens.UUCP> matthew at sunpix.UUCP ( Sun NCAA) writes:
<I'm not sure about the validity of the construct 'chcnt++ += (c == '\n');', but
<I'd write your program scrap as:
<        while ( (c = getchar) != EOF)
<        {   if (c == '\n')
<                chcnt++;
<            chcnt++;
<        }
<(Before closing, there is one logic flaw in your scrap of code. '(c == '\n')'
<does not have to evaluate to an integer 1. It must only be a non-zero value if
<true.)

I beg to differ. (c == '\n') is *guaranteed* to evaluate to 1 or 0, and
nothing else. Thus, this is guaranteed to work:
	int c;		/* c needs to be an int, not a char	*/
 	while ( (c = getchar()) != EOF)
 		chcnt += 1 + (c == '\n');



More information about the Comp.lang.c mailing list