Path: utzoo!utgpu!utcsri!flaps From: flaps@utcsri.UUCP (Alan J Rosenthal) Newsgroups: comp.lang.c Subject: Re: == vs = Message-ID: <5981@utcsri.UUCP> Date: 11 Apr 88 05:41:09 GMT Article-I.D.: utcsri.5981 Posted: Mon Apr 11 01:41:09 1988 References: <11216@brl-adm.ARPA> <2111@chinet.UUCP> <4403@garfield.UUCP> <226@hotlr.ATT> <130@obie.UUCP> Reply-To: flaps@dgp.toronto.edu (Alan J Rosenthal) Organization: University of Toronto Lines: 26 Summary: In article <130@obie.UUCP> wes@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."