[comp.lang.c] Using `=' in a conditional

bevan@cs.man.ac.uk (Stephen J Bevan) (03/18/91)

In <775@camco.Celestial.COM> bill@camco.Celestial.COM (Bill Campbell) writes :
]In <15481@smoke.brl.mil> gwyn@smoke.brl.mil (Doug Gwyn) writes:
]
]>In article <8148@rsiatl.Dixie.Com> stan@Dixie.Com (Stan Brown) writes:
]>>>"if (a = b)" instead of "if (a == b)". 
]>>... I have yet to see a UNIX compiler complain about it
]
]>That's good, because it is valid C and the compiler cannot know whether
]>or not it reflects the programmer's intentions.
]
]My personal preferance would be a WARNING message.  Certainly it
]is a legal construction, but 90% of the time when I do this it
]was my mistake!

This warning had better be optional.  Also if you really want a
warning, I'd like it to be a bit more selective than just warning
about an assignment, I'd like to warn depending on what was being
assigned.  For example I view the following as quite different cases :-

  if (a= b) ...
  if (a= some_fun()) ...

I've never actually done the former, but I use the latter regularly.
Note the way the assignment is written.  I remember reading about `='
vs `==' before I started using C and decided to follow some advice to
write assigment assymetrically i.e.  `a= b' rather than `a = b'.

For what its worth, I haven't been programming in C that long (3 - 4
years), but I can't actually remember using a `=' when I meant `=='.
I've made plenty of other mistakes though :-)

bevan

richard@iesd.auc.dk (Richard Flamsholt S0rensen) (03/18/91)

>>>>> On 17 Mar 91 19:26:22 GMT, bevan@cs.man.ac.uk (Stephen J Bevan) said:
Stephen> For what its worth, I haven't been programming in C that long (3 - 4
Stephen> years), but I can't actually remember using a `=' when I meant `=='.

  The keyboard on Sun's SparcStations are quite slow; I've often
writen stuf containing only one letter (oops, mised that one ;-) where
it should have been a double letter and that's bit me once or twice
with the == versus = .

  Just my $0.02: using "if (x)" signals, that we're testing the
boolean value of x rather than testing whether x != 0. The reason for
using "if (x != 0)" is exactly the same reason why you write NULL or
'\0' instead of plain 0 always; you want your code to syntactically
signal your intensions.

--
/Richard Flamsholt
richard@iesd.auc.dk