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.
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.