Noeud:The First Bug, Noeud « Next »:Try Again..., Noeud « Previous »:Examining Variables - Print, Noeud « Up »:An example debugging session using gdb
The value of argc
was correct, so lets move on a step and see what happens.
(gdb) next 20 printf( "Usage: ecount <word>\n"); (gdb) list 15 16 /* check we have a parameter */ 17 18 if( argc = 1 ) 19 { 20 printf( "Usage: ecount <word>\n"); 21 exit(1); 22 } 23 24 /* Make our own local copy of argv[1] */ (gdb) print argc $2 = 1 (gdb)
Here we see that execution moved into the if()
statement, which we didn't expect based on the value of argc
. I have listed the code to remind myself of the surrounding structure, then checked the value of argc
again.
argc
is now 1, not 2! Looking closer at the code we see that the comparison operator in the if()
statement was incorrectly written as =
instead of ==
. This assigned 1 to argc
and returned 1 as the expression value, so the if
evaluated as true and execution went into the if
block.