Path: utzoo!attcan!uunet!seismo!sundc!pitstop!sun!quintus!ok From: ok@quintus.uucp (Richard A. O'Keefe) Newsgroups: comp.lang.c Subject: Re: down Message-ID: <856@quintus.UUCP> Date: 13 Dec 88 09:43:13 GMT References: <843@quintus.UUCP> <45476@yale-celray.yale.UUCP> Sender: news@quintus.UUCP Reply-To: ok@quintus.UUCP (Richard A. O'Keefe) Organization: Quintus Computer Systems, Inc. Lines: 26 In article <45476@yale-celray.yale.UUCP> wald-david@CS.YALE.EDU (david wald) writes: >In article <843@quintus.UUCP> ok@quintus.UUCP (Richard A. O'Keefe) writes: >> for (chcnt = 0; (c = getchar()) != EOF; chcnt += c == '\n' ? 2 : 1) >> ; > >Urgh. Yes, it's not *too* unreadable, yes it's correct, but if you're >going to do it like that, why bother eliminating the while loop: But I *didn't* eliminate any "while" loop. I started again from scratch and observed that the point of the loop is to maintain chcnt, and a "for" loop was the appropriate structure. In fact, the way I would be inclined to do this is a wee bit more general: char chlen[256]; for (c = sizeof chlen; --c >= 0; ) chlen[c] = 1; chlen['\n'] = 2; for (chcnt = 0; (c = getchar()) != EOF; chcnt += chlen[c]) ; The advantage of this is that you can also add chlen['\r'] = 0; so that the same result will be obtained from a file which already has CRLF pairs as from one which has single LFs.