[comp.lang.c] Simple C Question

furlani@broadway.UUCP (John L. Furlani) (12/11/88)

In article <gables.352@umigw.miami.edu>, slores%gables.span@umigw.miami.edu (Stanislaw L. Olejniczak) writes:
> PLEASE don't flame for posting too simple a question.  I think the following
> SHOULD work, but will not even compile:
> 	while ( (c = getchar) != EOF)
> 		chcnt++ += (c == '\n');
> The purpose is to count characters in a file.  Each time a newline is


    This snippit of code should do it.  It will count the newlines, but it
won't count the returns.  

        while ((c = getc(infile)) != EOF)
            if (c == '\r') chcnt++;

    As for 'chcnt++ += (c == '\n')', you can't assign what is being 
incremented to anything.  This is different of course from '*count++ = 45'
where the pointer is being incremented and the info contained in the 
pointer is being assigned.


Flame?  Who us?

____________
Disclaimer:  "It's Mine! Mine! All Mine!!"
John L. Furlani 
The University of South Carolina, Columbia SC
(...!uunet!ncrlnk!ncrcae!broadway!furlani)

gwyn@smoke.BRL.MIL (Doug Gwyn ) (12/13/88)

In article <192@broadway.UUCP> furlani@broadway.UUCP (John L. Furlani) writes:
-    This snippit of code should do it.  It will count the newlines, but it
-won't count the returns.  
-        while ((c = getc(infile)) != EOF)
-            if (c == '\r') chcnt++;

Oh, really?

jrll@Portia.Stanford.EDU (john ralls) (12/15/88)

In article <192@broadway.UUCP> furlani@broadway.UUCP (John L. Furlani) writes:
>
>In article <gables.352@umigw.miami.edu>, 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

byron@pyr.gatech.EDU (Byron A Jeff) (12/19/88)

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 <gables.352@umigw.miami.edu>, 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

ark@alice.UUCP (Andrew Koenig) (12/19/88)

In article <6959@pyr.gatech.EDU>, byron@pyr.gatech.EDU (Byron A Jeff) writes:

> 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?

If the problem is stated this way:

	count all the characters and count one extra for each newline

then it seems to me that the most direct solution is:

	chcnt++;
	if (c == '\n')
		chcnt++;

I suspect this is also the fastest on most machines.  For example,
here are instruction counts for the compiler on my machine:

	c=='\n'		c!='\n'		statement

	6		6		chcnt += (c == '\n') ? 2 : 1;
	5		4		chcnt += 1 + (c == '\n');
	4		3		chcnt++;
					if (c == '\n')
						chcnt++;

This assumes that c and chcnt are both in registers.
-- 
				--Andrew Koenig
				  ark@europa.att.com

mat@mole-end.UUCP (Mark A Terribile) (12/21/88)

> 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?

I certainly hope that the economy or practicality of your program does not
hinge on this!  What's more important, I think, is that unless you have good
reason for using tricks (e.g. you are writing a ``compiler'' that produces C)
your second form

	... + ( c == n )

works because [ 0 1 ] is the ordered set of things you want to
AND COINCIDENTALLY [ 0 1 ] is the range of the C equality operators.

Making your code depend on such a coincidence forces the reader to be mindful
of the coincidence.  If the code is human-written to be human-read, I believe
that it's very poor practice.

Besides, any respectable compiler should generate the same code for both,
modulo the assembly language label names (if such there be).
-- 

(This man's opinions are his own.)
From mole-end				Mark Terribile

gwyn@smoke.BRL.MIL (Doug Gwyn ) (12/25/88)

In article <6959@pyr.gatech.EDU> byron@pyr.UUCP (Byron A Jeff) writes:
>Any comments one which one of the ones we've seen is most efficient?

Use the obvious

	++chcnt;

	if ( c == '\n' )
		++chcnt;	/* for implied CR */