UMBC CS 201, Fall 02
UMBC CMSC 201 Fall '02 CSEE | 201 | 201 F'02 | lectures | news | help

Debugging Techniques

We've talked about a little already about how to use printf() statements to debug your programs, and by now all of you realize that it usually takes more time to debug a program than it does to write it. The good news is, as you gain more experience you'll make fewer logic and syntax errors. You'll also get better at debugging your own code. The bad news is, it's going to take a lot of experience.

Today we're going to discuss some basic debugging techniques, including the use of a symbolic debugger to help you find and fix errors in your code.

Read the Compiler Errors

Messages like :

linux3[77] % gcc -Wall -ansi sample.c sample.c: In function `main': sample.c:30: parse error before `printf' sample.c:36: warning: control reaches end of non-void function linux3[78] % tell you where to look.

Here are the lines of sample.c from line 26 to 36 :

/* Display the largest integer entered */ if(largest == 0) { printf("\nYou quit right away! No integer to") printf(" display.\n"); } else { printf("\nThe largest integer entered was %d.\n", largest); } } The first error : sample.c:30: parse error before `printf' Says before the word printf on line 30 there was a parse error. Since printf is the first word on line 30, you need to examine the previous line. Sure enough there is a missing semicolon on line 29.

There is also a warning message:

sample.c:36: warning: control reaches end of non-void function Line 36 is the closing brace of main and the warning means that something needs to be returned instead of the function just ending. Adding return 0; will fix this problem.

Instead of cutting or commenting out every line that comes up with an error, it's important to try to understand what's causing that error.

But How Do I Get My Editor To Tell Me What Line Number I'm On?

In xemacs, the command [ESC]-x line (press escape, press x, then type the word line and hit enter) will turn on the line numbering. The line you're on will appear on the bottom bar. Like this :

ISO8--**-XEmacs: sample.c (C Font)----L30--All----------------

To jump to a line, the command [ESC]-x goto line (press escape, press x, then type the two words goto line and hit enter) will prompt you for the line number you want to go to. Then type in the line number and hit enter.

In vi, the command [CTRL]->g (hold down the control key while pressing "g") will show you what line you are on. To jump to a line, type in the line number and then press "G".


CSEE | 201 | 201 F'02 | lectures | news | help

Monday, 11-Nov-2002 17:40:24 EST