== vs =

Alan J Rosenthal flaps at utcsri.UUCP
Mon Apr 11 15:41:09 AEST 1988


In article <130 at obie.UUCP> wes at obie.UUCP (Barnacle Wes) writes:
>The one thing that everyone seems to have forgotten in this discussion
>is that `=' is NOT always invalid in an if clause:
>
>    if (status = *statreg) {
 [ where what is intended is assignment followed by testing of the result ]

I think this is better written as "if ((status = *statreg))"; some
people even recommend "if ((status = *statreg), status)".%

If you write the if as "if (a = b)", many readers of your code
(possibly including yourself) will first think "aha, this is a typo".
Many other readers will read it as "if (a == b)".  It is therefore a
good idea to use a visually distinct form.  The two forms I list above
both result from the idea that `=' should not be the top level operator
in a conditional expression.

ajr

%  The semantics of this second version differs from the original if
   `status' is volatile.  If you use volatile in a way that could come
   up here, you might not want to adopt this second syntax.

--
"An aardvark in the nose is worth two in the phototypesetter."



More information about the Comp.lang.c mailing list