C
printf debugging
Learn printf debugging tactics.
Tracing with printf
Sometimes gdb is impractical; for instance the program is very slow or runs on a server. In those cases printf debugging is the fastest path. You sprinkle temporary printf or fprintf(stderr, ...) calls into the code.
Example: fprintf(stderr, "trace: start n=%d\n", n);. Using stderr keeps tracing separate from normal output; you can redirect it with ./prog 2> log.txt. A good trace message includes the function name and the values of key variables. Once the bug is found, these calls are removed; they do not stay in production code.
Function entry and exit
To check whether a function is called and with what values, you place trace lines at entry and exit. This shows the function's flow from start to finish.
Example: int process(int n) { fprintf(stderr, "trace: start n=%d\n", n); int result = n * 2; fprintf(stderr, "trace: end result=%d\n", result); return result; }. The entry message shows parameters, the exit message shows the return value. When main calls this function and prints the result, you see both the trace and the result together. This pattern helps find which layer holds the bug, especially in deep call chains.
Exercise Write a traced function
Write int process(int n). At entry print fprintf(stderr, "iz: başla n=%d\n", n);, compute int result = n * 2;, at exit print fprintf(stderr, "iz: bit result=%d\n", result); and return result;. In main call it with printf("%d\n", process(5));.
Show example solution
#include <stdio.h>
int process(int n) {
fprintf(stderr, "iz: başla n=%d\n", n);
int result = n * 2;
fprintf(stderr, "iz: bit result=%d\n", result);
return result;
}
int main(void) {
printf("%d\n", process(5));
return 0;
}
Test yourself
-
Match the tracing patterns
- fprintf(stderr,...)
- Trace to error stream
- printf(...)
- Trace to stdout
- 2> log.txt
- Redirect trace to file
- iz: başla
- Function entry
-
Complete the trace output
fprintf(stderr, "iz: başla n=%d
", ____);
Show answer
fprintf(stderr, "iz: başla n=%d
", n);
-
Why does printf debugging use stderr?
- Because stdout is unavailable
- Because stderr is faster
- Because stderr is separate from stdout
- Because stderr speeds up the compiler
Show answer
Because stderr is separate from stdout — stderr is separate from stdout, so tracing stays apart from normal output.
Next lesson: Summary - Debugging