In the previous lesson you found the copy-paste bug by spotting -9999 in the output. That worked because the program was tiny. In real programs you need a way to stop the program part-way through and look inside it. That tool is the debugger, and Code::Blocks has one built in. In this lesson you use it to watch the copy-paste bug happen, one line at a time.
In this lesson you will
- Set a breakpoint and start a program in Debug mode
- Step through code with Next line and Step into, and jump ahead with Continue
- Inspect variables in the Watches window
- Discover that a
floatcannot store 15.1 exactly
Try it: set up the buggy program
Open 002-UserInputCProgram and put the copy-paste bug back in, so that the second scanf reads into &x:
#include <stdio.h>
#include <stdlib.h>
int main()
{
float x = -9999;
float y = -9999;
printf("Please enter a value for x:\n");
scanf("%f", &x);
printf("Please enter a value for y:\n");
scanf("%f", &x);
float sum = x + y;
printf("The sum of %.2f and %.2f is %.2f\n", x, y, sum);
return 0;
}
Check that the build target in the toolbar is set to Debug (not Release). The Debug target builds your program with extra information (GCC’s -g option) that lets the debugger match the running program to your lines of code.
Try it: breakpoints
A breakpoint is a marker that says “pause here”. Click in the margin just to the right of the line number beside the first scanf (line 10). A red dot appears. (You can also put the cursor on the line and press F5 to toggle a breakpoint.) Add a second breakpoint on the line float sum = x + y;.
Break it on purpose: breakpoints that are never hit
With both breakpoints set, click the usual Build and run button. Observe: the program runs from start to finish as if the breakpoints were not there.
Breakpoints only work when the program runs under the debugger. Stop the program. Then choose Debug → Start / Continue (F8, or the red arrow on the debugger toolbar).
This time the program starts, prints the first prompt, and stops. A yellow arrow appears in the margin at your first breaThe yellow arrow marks the line that will run next. It has not run yet. That is why nothing has been typed into the console so far.
Try it: step and watch
Open Debug → Debugging windows → Watches. It lists the local variables and their current values. Right now x and y both show -9999.
- Choose Debug → Next line (F7). The
scanfruns and waits for you, so switch to the console, type15.1and press Enter. The yellow arrow moves down one line. - Look at the Watches window.
xnow shows something like15.1000004. Keep that in mind for later. - Choose Debug → Start / Continue again. The program runs until it reaches the next breakpoint. On the way it asks for y: type
300. - Look at the Watches window again.
xhas changed to300, butyis still-9999. You have just watched the bug happen: the value meant for y went into x. - Choose Debug → Stop debugger, fix the second
scanfto use&y, and debug once more to confirm that both values arrive where they should.
What just happened
The debugger gives you three ways to move forward:
- Next line (often called step over) runs the current line completely, then pauses on the next one. If the line calls a function, the whole function runs in one go.
- Step into (Shift+F7) also runs one line, but if that line calls a function you wrote, it goes inside the function and pauses on its first line. This becomes useful in the next lesson, when you write your own functions. Library functions such as
printfhave no source code for the debugger to show, so stepping into them usually behaves like Next line. - Continue runs at full speed until the next breakpoint, or until the program ends.
One more surprise: when the program finishes under the debugger, the console window closes straight away, before you can read the output. A simple fix is to put a breakpoint on the return 0; line. The program then pauses there, with all of its output still on screen.
Name it: floats are not exact
Why did the Watches window show 15.1000004 instead of 15.1? A float stores numbers in binary, and most decimal fractions, including 0.1, cannot be written exactly in binary. In the same way, 1/3 cannot be written exactly in decimal (0.3333…). The computer stores the nearest value it can. You can see the stored value by asking printf for more decimal places than usual:
#include <stdio.h>
#include <stdlib.h>
int main()
{
float x = 15.1;
printf("%.2f\n", x);
printf("%.10f\n", x);
return 0;
}
15.10 15.1000003815
For everyday output, %.2f hides the tiny difference. Just remember never to expect a float to be exactly equal to a decimal value like 15.1. This matters later, when you start comparing values.
Key terms
- Debugger: a tool that runs your program under control, so you can pause it and inspect it. Code::Blocks uses GDB.
- Breakpoint: a marker on a line where the program pauses when run in Debug mode.
- Next line (step over): run the current line, including any function it calls, and pause on the next line.
- Step into: if the current line calls one of your functions, pause inside that function.
- Continue: run until the next breakpoint.
- Watches: a window showing the current values of variables.
- Debug target: a build configuration that includes debugging information (
-g).
Quick check
You set a breakpoint, click Build and run, and the program never pauses. Why?
Show answer
Breakpoints only work under the debugger. Start the program with Debug → Start / Continue.
The yellow arrow points at
y = 6;. Hasybeen set to 6 yet?Show answer
No. The arrow marks the line that will run next. Choose Next line, and then
ybecomes 6.What is the difference between Next line and Step into on a line that calls one of your own functions?
Show answer
Next line runs the whole function and stops on the following line. Step into goes inside the function and stops on its first line.
Why might the Watches window show
2.0999999for afloatset to 2.1?Show answer
Most decimal fractions cannot be stored exactly in binary, so the float holds the closest value it can.
Optional: a recorded demonstration of these steps starts around 2:10. Next line and Step into are demonstrated from about 18:00.
Part of the Introduction to C series. Next: Your First Function: void, Declarations, and Definitions