comma operator

Steven Weintraub stevenw at oakhill.UUCP
Wed Aug 16 00:24:00 AEST 1989


Before I go on, I would like to thank everyone who has responded so
far (both publically and email).  But now to the question.

In article <9775 at alice.UUCP>, ark at alice.UUCP (Andrew Koenig) writes:
> In article <2300 at oakhill.UUCP>, stevenw at oakhill.UUCP (Steven Weintraub) writes:
> 
> > while ((ch = getchar()), ch != EOF && ch != '\n')
> 
> I don't see any obvious reason that this is better than
> 
> 	while ((ch = getchar()) != EOF && ch != '\n')

Let me turn this around.  I have considered using this form before
and find it less satisfying than the original I posted.  It should
be pointed out that in both cases the code generated is the same,
so there is no gain or loss with either method.  I personally still
prefer my form better.  I think it is clearer that two functions are
happening here (assigning ch and testing against the exit condition).
And while I hate to disagree with the likes of Doug Gwyn and Kenneth
Arromdee, I believe I will still stick with my earlier form.

Now to dispense with some forms sent to me by email:

2 people sent me :
    
    for (ch = getchar();ch != EOF && ch != '\n';ch = getchar())

This is not preferred for the simple reason it will generate the
getchar twice.  If, as is often the case in the real word, you
are using another routine or macro to fetch the characters (not
one of the system macros) this can cause a lot of overhead.

As for the comment that the parenthesis around the ch = getchar() are 
not necessary.  This I know.  But I was raised to believe that an
extra amount of caution (especially in matters of precedence) is never
wasted.  If it doesn't generate extra code or confuse the programmer,
I usually put them in.

                   enough from this mooncalf - Steven
----------------------------------------------------------------------------
These opinions aren't necessarily Motorola's or Remora's - but I'd like to
think we share some common views.
----------------------------------------------------------------------------
Steven R Weintraub                             | O Lord,
...!cs.utexas.edu!oakhill!stevenw              |   let me talk gently,
Motorola Inc.  Austin, Texas                   | for I might have to eat my
(512) 891-3023 (office) (512) 453-6953 (home)  |   words tomorrow.
----------------------------------------------------------------------------



More information about the Comp.lang.c mailing list