Simple C Question (was: down)

john ralls jrll at Portia.Stanford.EDU
Thu Dec 15 08:05:05 AEST 1988


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



More information about the Comp.lang.c mailing list