Print/printf debugging is an often-used tool, but in order to use it in CS 24
you may need to alter your practices slightly.
TL;DR
1
2
3
4
// replace this:
printf("format string: %s %d\n", "string", 5); // my tests broke, and nothing happened!
// with this:
fprintf(stderr, "format string: %s %d\n", "string", 5); // shows up in terminal, bug solved
Why?
In CS 24, tests are often run by compiling your code as part of a larger test
program, which then produces output to be judged for correctness. This output
is written using printf (or similar) to an output stream called stdout.
Normally, stdout would show up in your terminal. However, when tests are run,
stdout is “captured” to be compared against the expected output for a correct
program, so printf doesn’t appear in the terminal. We can use
fprintf(stderr, ...) to print to stderr rather than stdout. stderr is
not captured when tests run, so the debug line will print to your terminal!