[comp.lang.c] Priorities of = and == again

maart@ark.UUCP (01/16/88)

Lately I asked why the priorities of = and == were not reversed:

	while ((c = getchar()) == ' ')

is used much more than

	while (c = getchar() == ' ')
.
I argued that ONLY programs containing code of the second form,
need to be rewritten using parentheses; occurrences of those
expressions are not hard to find using shelltools (SUN, yeah).
Concluding, I'm not very impressed by the 'backward compatibility'
argument used by ANSI and by Doug Gwyn, who responded to my previous
article (thanks, Doug).
Any further thoughts on this ?
-- 
Time flies like an arrow, fruit flies |Maarten Litmaath @ Free U Amsterdam:
like an orange.      (seen elsewhere) |maart@cs.vu.nl, mcvax!botter!ark!maart

gwyn@brl-smoke.ARPA (Doug Gwyn ) (01/16/88)

In article <1171@ark.cs.vu.nl> maart@cs.vu.nl (Maarten Litmaath) writes:
>Concluding, I'm not very impressed by the 'backward compatibility'
>argument used by ANSI and by Doug Gwyn...

I take it you're volunteering to edit everyone's code to make the
change you propose?

marti@ethz.UUCP (Robert Marti) (01/18/88)

In article <7115@brl-smoke.ARPA>, gwyn@brl-smoke.ARPA (Doug Gwyn ) writes:
> In article <1171@ark.cs.vu.nl> maart@cs.vu.nl (Maarten Litmaath) writes:
> >Concluding, I'm not very impressed by the 'backward compatibility'
> >argument used by ANSI and by Doug Gwyn...
> 
> I take it you're volunteering to edit everyone's code to make the
> change you propose?

C'mon, replacing = by := (or <- or whatever) and == by = shouldn't be
all that difficult with all those UNIX tools (sed, awk, etc.).  The
real problem is re-educating all those C hackers  ;-) ;-)

Sorry, just couldn't resist.  Flames to /dev/null ...

-- 
Robert Marti                    Phone:       +41 1 256 52 36
Institut fur Informatik
ETH Zentrum/SOT                 CSNET/ARPA:  marti%ifi.ethz.ch@relay.cs.net
CH-8092 Zurich, Switzerland     UUCP:        ...uunet!mcvax!ethz!marti

edw@IUS1.CS.CMU.EDU (Eddie Wyatt) (01/18/88)

In article <1171@ark.cs.vu.nl>, maart@cs.vu.nl (Maarten Litmaath) writes:
> Lately I asked why the priorities of = and == were not reversed:
> 
> 	while ((c = getchar()) == ' ')
> 
> is used much more than
> 
> 	while (c = getchar() == ' ')
> .
> I argued that ONLY programs containing code of the second form,
> need to be rewritten using parentheses; occurrences of those
> expressions are not hard to find using shelltools (SUN, yeah).
> Concluding, I'm not very impressed by the 'backward compatibility'
> argument used by ANSI and by Doug Gwyn, who responded to my previous
> article (thanks, Doug).
> Any further thoughts on this ?

   Changing the precedence rules will will break more than just
those while loops that you are complaining about.  Consider

	found = key == entry.key;

Perfectly valid and it is pretty obviously that the programmer
didn't intend the expression to be parsed as:

	(found = key) == entry.key;

which is what would happen if you change the precedence of "=" to be
higher than "==".  Then again, we could change it such that in the
context of either "while" or odd number Tuesdays of an even number
month "=" has a  higher precedent than "==". :-)

BTW I usually parenthesises expressions involving boolean assignment
to make it clearly as to what is going on:

	found = (key == entry.key);


-- 

Eddie Wyatt 				e-mail: edw@ius1.cs.cmu.edu

flaps@dgp.toronto.edu (Alan J Rosenthal) (01/26/88)

In article <1171@ark.cs.vu.nl> maart@cs.vu.nl (Maarten Litmaath) writes:
>Lately I asked why the priorities of = and == were not reversed:
...
>I argued that ONLY programs containing code of the second form,
>need to be rewritten using parentheses; occurrences of those
>expressions are not hard to find using [any standard unix tools].

This is a bad idea.  Gratuitious changes are bad.  Things change enough
without changes just for the sake of change.