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
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

Debugging with GDB | C Programming Fundamentals lesson | BraveProgrammer
  1. Courses
  2. /
  3. C Programming Fundamentals

Lesson 45 of 50

·c

Debugging with GDB

12 mins

Course progress90%

What you'll learn

Keep these ideas in mind as you work through the lesson.

5 points
  • Compile with `-g` to generate debugging symbols.
  • `break` sets a breakpoint; `run` starts execution.
  • `next` steps over a function, `step` steps into it.
  • `print` displays the current value of a variable.
  • `backtrace` (`bt`) shows the stack trace for crashes.

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
", 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.
Make it stickPractice what you've learned1 quiz · 3 practical tests · Optional activities from C Programming FundamentalsBrowse activities
Previous lessonError Handling – `errno` & `perror`Continue to lesson 46Unit Testing with CMockaBack to C Programming Fundamentals curriculum