I've tried searching the documentation and the internet as best as I'm able, but I haven't been able to get the Xcode compiler to issue a warning if the assignment operator is used in an if statement.
I'm coming from RealBasic, where I've got an extremely strong habit of typing this sort of comparison:
if x = 5 then ...
In C, of course, that syntax assigns x the value of 5 then tests the result to see if it's nonzero, and the "correct" operator is:
if (x == 5) { ... }
I've found several mentions that the compiler should be able to warn if there is an assignment made in an if comparison, but I cannot find how to turn it on in Xcode 3.1/gcc. I found the -pedantic
option, but that didn't seem to generate the warning.
Since I've spent a fair amount of time twice now tracking down bugs that turned out to be "=" instead of "==", I'd like the help of a warning.
I know that I can do this instead (which will cause a compiler error):
if (5 = x) { ... }
...but that requires changing ingrained coding habits, too. Not to mention that it looks clumsy and backward.
Thanks!