What should be added to C ( really = in boolean context )

Gregory Smith greg at utcsri.UUCP
Mon Jun 2 01:58:15 AEST 1986


In article <1514 at mmintl.UUCP> franka at mmintl.UUCP (Frank Adams) writes:
>In article <852 at bentley.UUCP> kwh at bentley.UUCP writes:
>>>>o There should be an option to flag statements of the form if (v = e) ...
>>>>(Actually, I wouldn't be averse to a coding standard which forbade such
>>>>things, in favor of if (v = e, v) ...)
>>
>>Urgh.  Are you talking about removing the assignment-has-a-value feature
>>completely, or a special case for "if (v = e)"?  (I always write this as
>>"if ((v = e) != 0)" to make it clear that I didn't mean "if (v == e)".)
>>
>
>Actually, I was not proposing removing the assignment-has-a-value feature
>anywhere (although I would not put it into a new language I was designing).
>The proposal was that it be avoided in if's (and probably while's as well)
>as a coding standard.  That is, it would be legal, but not recommended.
>
Somebody mentioned that Lint could warn about using an assignment value
in a boolean context - unless you said /* BOOLEUSED */. I think the
warning is a good idea, but the new special comment is not. You can just
as easily write ( (v=e)!=0 ) and get same code on any non-silly compiler.
( Note I did not say "good optimizing compiler", which are widely believed
to be capable of witchcraft :-) ). ( v=e, v ) would probably get you the
same code, too - ditto ( v=e, v!=0). The comma-less version may be
a mite faster in some cases. The presence of a 0 encourages the programmer
to be explicit as to what kind of zero is being used:

		while( p=p->link, p != NULL ){ ...

I wouldn't mind a compiler warning if an assignment op were used in
*any* boolean context ( including being the operand of !, ||, && , and
the expr. before a ? ).  Note that boolean context is inherited by the
RHS of a ',' and by e2 and e3 in e1?e2:e3.

-- 
"We demand rigidly defined areas of doubt and uncertainty!" - Vroomfondel
----------------------------------------------------------------------
Greg Smith     University of Toronto      UUCP: ..utzoo!utcsri!greg



More information about the Comp.lang.c mailing list