Using `=' in a conditional (was Re: Funny mistake)

Stephen J Bevan bevan at cs.man.ac.uk
Mon Mar 18 05:26:22 AEST 1991


In <775 at camco.Celestial.COM> bill at camco.Celestial.COM (Bill Campbell) writes :
]In <15481 at smoke.brl.mil> gwyn at smoke.brl.mil (Doug Gwyn) writes:
]
]>In article <8148 at rsiatl.Dixie.Com> stan at 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



More information about the Comp.lang.c mailing list