Debugging C in GitHub Codespaces¶
A Step-by-Step Guide to Native Development¶
Prerequisites for C Development¶
- Extension: Install the C/C++ by Microsoft extension.
- We already installed this in the previous lesson. - Compiler: Ensure
gccorclangis available.
- Codespaces usually come with build-essential packages pre-installed. - Debugger:
gdb(GNU Debugger) is required for interactive debugging.
💡 Check Terminal: Run
gcc --versionto verify compilation tools are present.
Configuring the Project Structure¶
.vscode Configuration Files¶
To automate C compilation and debugging, create these files in the .vscode directory:
1. tasks.json (Build)
{
"version": "2.0.0",
"tasks": [
{
"label": "Build C Program",
"command": "gcc",
"args": [
"-g",
"hello.c",
"-o",
"hello"
],
"type": "shell"
}
]
}
2. c_cpp_properties.json (IntelliSense)
{
"configurations": [
{
"name": "Linux",
"compilerPath": "/usr/bin/gcc",
"cStandard": "c11",
"cppStandard": "c++11"
}
],
"version": 4
}
Tip: The
gcc -gflag ensures debug symbols are generated for the debugger.
Setting Up the Debug Session¶
launch.json Configuration¶
This file tells VS Code how to start the debugger.
Location: .vscode/launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug C Program",
"type": "cppdbg",
"request": "launch",
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb",
"cwd": "${workspaceFolder}",
"program": "${workspaceFolder}/hello",
"preLaunchTask": "Build C Program"
}
]
}
Auto-build: The
preLaunchTaskcompiles your code automatically before attaching the debugger.
Interactive Debugging Workflow¶
5 Steps to Debug C Code¶
- Set Breakpoints: Click the gutter next to lines in
main.c.
- Red dot = active breakpoint. - Start Debugging: Press
F5or click “Run and Debug”. - Step Over/Into: Use
Step Over(F10) orStep Into(F11). - Inspect Variables: Hover over pointers, structs, or arrays.
- Call Stack: View the execution path and return addresses.
Pointer Warning: When debugging pointers, ensure you inspect the address (
&var) vs the value (var).
Git Integration for C Projects¶
Managing Source Control in Codespaces¶
- Untracked Files: New C files (
main.c,debug.h) are marked asUntracked. - Staging: Stage changes via Source Control panel or
git add main.c. - Commit:
bash  git commit -m "fix: null pointer in memory allocation"  
- Push:
bash  git push  
Tip: Codespaces automatically syncs your working tree. If you break your code, you can
git checkoutto revert.
⚠ Common Pitfalls¶
Troubleshooting C Debugging¶
- No Symbols: Ensure
-gflag is used during compilation.
- Fix: Updatetasks.jsonto include-g. - GDB Path: Sometimes
gdbisn’t linked inlaunch.json.
- Fix: Verify path withwhich gdb. - Port Forwarding: If your C program creates a server socket, forward the port.
- Fix: Go toPort Statusand click the “Open in Browser” icon. - Async Code: Debug async C programs requires specific configurations.
- Fix: UseThreaddebugger options for multi-threaded C apps.
Summary & Next Steps¶
Recap¶
- Extension: Use C/C++ extension by Microsoft.
- Build: Configure
tasks.jsonwithgcc -g. - Debug: Configure
launch.jsonwith GDB. - Git: Commit changes to sync with GitHub.
Our Challenge¶
- Write a C program with a loop.
- Set a breakpoint inside the loop.
- Debug until you find the off-by-one error.
- Commit and push to GitHub.
Last update :
May 31, 2026
Created : May 13, 2026
Created : May 13, 2026