Debugging Code
Goal: Students run into many of the same errors. This is a quick reference page to help you find the problem by learning to understand the error message.
Common Compilation Errors
Illegal symbol: x
If you have an illegal symbol,
x
is probably a typo of an actual function. You are probably calling an stdio function by the wrong name. Some examples of typos:Printf, fscan, print
, etc.Illegal new line in string constant
You have a new line (an actual return, not a "\n") in a string. Find the first line that has this problem. The problem is probably that you did not terminate a string constant where you expected to. Use Emacs' syntax highlighting and formatting to help identify where your problem is.
Parse error
These are tricky: they usually mean that you forgot a semicolon at the end of a statement or you have misaligned braces. Find the first line in the code that has the parse error. Look above that line and check if you are missing a semicolon and make sure that you have matching braces.
- Tougher ones
This one had me held up for awhile:
function designator is not of function type
First, try compiling with gcc to see if you get a different error.
The problem is that you have a function and a variable with the same name. Tricky one, huh?
Runtime Errors
Segmentation Fault
These are memory access problems. They are often caused by errors reading input or printing output or accessing an array at an index that is out of bounds. Things to look for with input:
- Is the input source valid? (Did the input file actually open? Is the input file available?)
- Do the
scanf
arguments include variables with the&
preceding them? - Does the
scanf
argument match the datatype of the specification format?
Things to look for with output:
- Are you printing to a valid file pointer?
Things to look for with arrays:
- Are you accessing the array with an index that is out of bounds: less than 0 or greater than the size of the array.
Another cause of segmentation faults is too many recursive function calls. Add print statements to find out if your function ever returns.
Debugging Hints
When I look at your code, I look for a few things:
- variables are declared the correct data type and are initialized
- braces are correct -- braces match up and enclose the correct blocks of code
- printf and scanf statements use the right specification that represent the variable's datatypes
- your algorithm's logic is correct. I will work through your code--on paper sometimes--to figure out if your algorithm works as expected
- functions are called with appropriate variables
- function return the right datatype, variable
Some more suggestions:
- Break up your code into logical sections
Your code is usually made up of several pieces. For example, you may read in input, process the input, then print the output. Look at each of these sections independently, and make sure that each section works as you expect
- Add print statements
When you are having trouble and you can't think through the code, you can try adding print statements. (Make sure your print statements are correct!) You can use the print statements to figure out how far you're getting in your code so that you can determine where a segmentation fault occurs.
Other common problems and solutions? Email and I'll share your hints on this page.