Lesson 36 / 36

Debugging Basics

Find bugs with printf, warnings, and gdb.

printf debugging

The simplest technique: sprinkle printf calls to print variable values and confirm which lines actually run. Fast, but requires editing and recompiling each time.

Compile with debug symbols

-g embeds debug info gdb needs; -Wall -Wextra surfaces suspicious code before you even run it.

gcc -g -Wall -Wextra prog.c -o prog
gdb ./prog

A minimal gdb session

Inside gdb: break main sets a breakpoint, run starts the program, next steps line by line, and print x inspects a variable's current value.