C bugs often involve memory and undefined behavior. Combine compiler warnings, printf tracing, and gdb (or lldb) for interactive debugging.
gdb essentials
gcc -g main.c -o main
gdb ./main
(gdb) break main
(gdb) run
(gdb) print variable
(gdb) next
Sanitizers (local)
-fsanitize=address catches many heap overflows and use-after-free issues during development—use alongside Valgrind where available.
Important interview questions and answers
- Q: Segmentation fault?
A: Invalid memory access—null deref, buffer overflow, use-after-free. - Q: Why compile with -g?
A: Maps machine code back to source lines in the debugger.
Self-check
- What gdb command sets a breakpoint?
- Name one tool that detects heap corruption.
Tip: Compile with -g -fsanitize=address,undefined during development—catches many pointer bugs before they ship.
Interview prep
- Segfault causes?
Null dereference, buffer overflow, use-after-free, stack overflow—use gdb and sanitizers to locate.