0Pricing
C Academy · Lesson

Debugging with GDB

Use the GNU Debugger (gdb) to analyze and fix issues in your programs.

Debugging with GDB is a free C Academy lesson on CoddyKit — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

1

Debugging with GDB

The GNU Debugger (GDB) is a powerful tool for finding and fixing bugs in C programs.

In this lesson, you will learn:

  • How to compile programs for debugging.
  • How to set breakpoints and inspect variables.
  • How to step through code execution.
Debugging with GDB — illustration 1

2

Compiling a Program for Debugging

To use GDB, compile your program with the -g flag:

gcc -g program.c -o program

This includes debugging information in the executable.

3

Starting GDB

To run a program in GDB:

gdb ./program

Once inside GDB, you can set breakpoints and inspect the program's execution.

4

Setting Breakpoints

A breakpoint stops the program at a specific line, allowing you to inspect variables and execution.

To set a breakpoint:

break main

This stops execution at the beginning of the main() function.

5

Example: Using GDB to Debug a Program

This program contains a bug that we will debug using GDB.

#include <stdio.h>

int main() {
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);
    printf("You entered: %d\n", num);
    return 0;
}

6

Running the Program in GDB

After setting a breakpoint, run the program using:

run

The program stops at the breakpoint, allowing you to inspect variables.

7

Inspecting Variables

Use the print command to check variable values:

print num

This helps identify incorrect values during execution.

8

9

Stepping Through Code

Use next to execute the next line of code.

Use step to enter function calls for deeper inspection.

10

Summary

In this lesson, you learned:

  • How to compile and run a program in GDB.
  • How to set breakpoints and inspect variables.
  • How to step through code execution.

Next, we will explore common runtime errors in C!

Debugging with GDB — illustration 10

Frequently asked questions

Is the “Debugging with GDB” lesson free?

Yes — the full text of “Debugging with GDB” is free to read here on the web, and the C Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C Academy course, upgrade to CoddyKit PRO.

What will I learn in “Debugging with GDB”?

Use the GNU Debugger (gdb) to analyze and fix issues in your programs. You practise C Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C Academy?

No prior experience is required. C Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Debugging with GDB” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C Academy lesson?

Yes. Every C Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Debugging with GDB
  2. Common Runtime Errors
  3. Exception Handling in C
← Back to C Academy