Path: utzoo!utgpu!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!ukma!gatech!gitpyr!byron From: byron@pyr.gatech.EDU (Byron A Jeff) Newsgroups: comp.lang.c Subject: Re: Simple C Question (was: down) Message-ID: <6959@pyr.gatech.EDU> Date: 18 Dec 88 23:34:22 GMT References: <192@broadway.UUCP> <4385@Portia.Stanford.EDU> Reply-To: byron@pyr.UUCP (Byron A Jeff) Organization: Georgia Institute of Technology Lines: 33 In article <4385@Portia.Stanford.EDU> jrll@Portia.stanford.edu (john ralls) writes: -In article <192@broadway.UUCP> furlani@broadway.UUCP (John L. Furlani) writes: -> ->In article , slores%gables.span@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@pyr.gatech.edu uucp: ...!gatech!pyr!byron