Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons

© 2026 BraveProgrammer. All rights reserved.

  1. Courses
  2. /
  3. C Programming Fundamentals

Lesson 45 of 50 · c

Debugging with GDB

Duration: 12 mins

The GNU Debugger (gdb) lets you step through code, inspect variables, and set breakpoints.

Typical workflow:

  1. Compile with -g flag to embed debug symbols.
  2. Start debugging: gdb ./a.out.
  3. Set breakpoints: break main or break file.c:line.
  4. Run program: run arg1 arg2.
  5. Step: next (line‑wise) or step (into functions).
  6. Inspect: print var, info locals.
  7. Continue: continue.
  8. Quit: quit.

📚 Example Session (pseudo‑output)

$ gcc -g -o sum sum.c
$ gdb ./sum
(gdb) break main
Breakpoint 1 at 0x4005b6: file sum.c, line 5.
(gdb) run 5 7
Starting program: ./sum 5 7
[New Thread 0x7ffff7fd4700 (LWP 1234)]
Breakpoint 1, main (argc=3, argv=0x7fffffffdfd8) at sum.c:5
5       int a = atoi(argv[1]);
(gdb) next
6       int b = atoi(argv[2]);
(gdb) next
7       printf("%d\n", a + b);
(gdb) print a
$1 = 5
(gdb) print b
$2 = 7
(gdb) continue
12
[Inferior 1 (process 1234) exited normally]
(gdb) quit

📚 Tips

  • Use layout src for a source window.
  • watch var stops when var changes.
  • backtrace (bt) shows the call stack after a crash.
Previous: Error Handling – `errno` & `perror`Next: Unit Testing with CMocka