6/19/2018 - 11:25 PM

Debugging for C in CS50 IDE

Debugging tools for C that we can use within CS50 IDE.

Debugging in C in CS50 IDE:

I. If the error occur when compiling, use "help50" to get more hint about the error.

Use help50 like this:

help50 make program_name

II. eprintf

Put "eprintf" in certain line of the code, so we know that a certain part of the code was excecuted.

Example code:

int x = get_int();
eprintf("Integer obtained!\n");

So when the program runs, the eprintf will tell you at what line of code this was done:

program_name.c:8: Integer Obtained!

So it also tells you the line number which is 8.

III. Debug50

So when a program does compile without error, but the output is not right, we use debug50.

First, we set a break point for the program, usually is on the line where the "main" is.

To set a break point, we put the cursor to the left of the "main" line, one click and a red dot will appear, that means it's the break point. So when the program runs, it will stop at the break point line.

You can double click on the red break point to make it go away.

To run using Debug50:

debug50 ./program_name

when this runs, the program will stop at the break point, and on the right a window will pop up.

In this window, it tells us what functions we are in, and what variables we have at that point, the value and type of that variable.

On top of this window, there are some keys we can use to control the running of the program.

  1. Rusme: which will let the program run to the end and ignoring any break points. Use this when we no longer want to debug.
  2. Step over: let the program execute the next line. So it will execute the next line and then stop at the next line after that.
  3. Step into: if there is a block of code, like a "if" with brackets, we can step into the brackets, and see how codes execute inside the brackets. Or if it's a function, also can step into it.
  4. Step out: to step out of the block of code or function that you have stepped into before.

So each time you step over a line, or step into a code block, take a look at the information about the functions involved in each step, and also how the value or type of the variables changed with the execution of each line. You can also change the value of a variable at a certain line, to see how the program behave. That would help us to find out where the problem might be.