Curious assignment behaviour

Andrew Dalke dalke at dalkescientific.com
Wed Oct 10 10:48:13 CEST 2001


Paul Rubin:
>The thing is, it's just not a matter of MY immunities.  I've spent
>plenty of time debugging other people's code too, and have encountered
>all kinds of errors in both their code and mine.  The =/== mistake has
>just been very rare in my experience.  Can it be that the Unix
>compilers I use flag the error a lot of the time, so it gets fixed
>quickly, while Windows compilers don't flag it, and you're mostly
>using Windows compilers?

You're kidding us, aren't you?  Or maybe you've only been programming
for the last few years.  When I started with unix (IRIX 3.3 in 1990)
it came with a K&R C compiler.  It didn't have any of this sort of
checking.  That's why 'lint' was written.

And you're saying you've never seen all the code that uses
    if (22 == a)
instead of
    if (a == 22)
just in case the '==' is accidently replaced with '='?  That's
common enough to be mentioned as a joke in Q5.A. of
  http://www.ddj.com/columns/stob/2001/0110vs001.htm
] When comparing variables with constants, write the constant
] first to avoid accidental assignment ie write

GCC was the first unix compiler I saw which generated a warning
for this problem.  That was about 4 or 5 years ago.  But trying
some compilers I have access to (some old C compilers too :)

> uname -a
SunOS day 5.7 Generic sun4d sparc SUNW,SPARCserver-1000
> cc -V
ucbcc: SC3.0.1 13 Jul 1994
  ...told you it was old :)
>
> cat > test.c
main() {
  int i = 0;
  if (i = 1) {
    printf("Hello.\n");
  }
}
> cc test.c
> ./a.out
Hello.
>

With "WorkShop Compilers 5.0 98/12/15 C 5.0" on "SunOS 5.7"
there is no warning.

With "MIPSpro Compilers: Version 7.2.1" on IRIX 6.4 there
*is* a warning.

Don't currently have ready access to other compilers.

                    Andrew
                    dalke at dalkescientific.com






More information about the Python-list mailing list