Owen Densmore wrote:
> So the *extremely* common bug  
> of using = rather than == causes entirely unexpected consequences ..  
> launching a missile.
>   
I expect to see both in many circumstances.
For example, when opening a file to read that is absent or to write, and 
write permission is forbidden.

if ((fp = fopen (filename, "r")) == NULL)
abort ();

I haven't really created many bugs because of "=" vs. "==". GCC even 
gives a warning...

$ cat t.c
#include <stdio.h>
#include <stdlib.h>

static void
launch_missle ()
{
printf ("Missle launched\n");
}

int
main (int argc, const char **argv)
{
int red_button_pressed = 0;

if (argc > 1)
red_button_pressed = atoi (argv[1]);

if (red_button_pressed = 1)
launch_missle ();

return 0;
}


$ gcc -Wall t.c
t.c: In function ‘main’:
t.c:18: warning: suggest parentheses around assignment used as truth value



============================================================
FRIAM Applied Complexity Group listserv
Meets Fridays 9a-11:30 at cafe at St. John's College
lectures, archives, unsubscribe, maps at http://www.friam.org

Reply via email to