Right, but if you compile with warnings as errors, then assignment within a condition won't compile, which prevents the issue.
I'd much rather go that way than write code that reads less like a human wrote it.
I'd much rather go that way than write code that reads less like a human wrote it.
$ cat test2.c
int main(int argc, char *argv[]) {
if (argc = 0)
return 1;
return 0;
}
$ make test
cc -Wall -ansi -pedantic test.c -o test
GCC version: gcc version 4.7.2 (Debian 4.7.2-5) huh@px:/tmp$ cat a.c
int main(int argc, char *argv[]) {
if (argc = 0)
return 1;
return 0;
}
huh@px:/tmp$ gcc -Wall a.c
a.c: In function 'main':
a.c:2: warning: suggest parentheses around assignment used as truth valueBut this is a such commonly-recognized pitfall that I actually don't know a single modern production compiler that does not generate this warning.
(edit) Just checked 4.7.1 and it generates the warning.
I will now go and hit myself with a LART for a bit to remember not to do that again.
There are two different ways to catch this class of bug. Both have value.