← Back to C++ Course | Chapter 17: Advanced C++ | Lesson 3 of 17

C++ Debugging Techniques

Debugging a C++ program combines print-statement tracing, compiler warnings, assert() checks, and a proper debugger like GDB to locate and understand the cause of unexpected behavior.
Syntax
cpp
#include <cassert>

assert(condition);   // aborts with an error if condition is false

std::cout << "variable = " << variable << std::endl;

Print-Statement Debugging

The simplest debugging technique is inserting temporary cout statements at key points in the code to print variable values and confirm which branches of logic actually execute.

Example: Print-Statement Debugging

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare x and set it to 5
	int x = 5;
	// Print to the console with cout
	std::cout << "x is: " << x << std::endl;
	// Check whether x > 0
	if (x > 0) {
		// Print to the console with cout
		std::cout << "Entered positive branch" << std::endl;
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Compiler Warnings

Compiling with warning flags like -Wall catches many bugs before the program even runs, flagging issues like uninitialized variables, comparisons that are always true, or unused values.

Example: Compiler Warnings

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	int x;
	x = 5;
	// Print to the console with cout
	std::cout << x << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Using assert()

The assert() macro from <cassert> checks that a condition is true at runtime, immediately terminating the program with a clear error message if it's ever false, catching bugs as close as possible to their source.

Example: Using assert()

cpp
// Include std::cout and std::cin
#include <iostream>
#include <cassert>

// Program execution starts in main()
int main() {
	// Declare age and set it to 25
	int age = 25;
	assert(age >= 0);
	// Print to the console with cout
	std::cout << "Assertion passed" << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Tracing with a Debugger

A debugger like GDB lets a program be paused at a specific line (a breakpoint), its variables inspected, and execution stepped through one line at a time, offering far more insight than print statements alone.

Example: Tracing with a Debugger

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare total and set it to 0
	int total = 0;
	// Loop: repeat while the condition holds
	for (int i = 0; i < 3; i++) {
		total += i;
	}
	// Print to the console with cout
	std::cout << total << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Common Bug Categories to Watch For

Common C++ bugs include off-by-one errors in loops, uninitialized variables, dangling pointers after delete, and comparing floating-point numbers for exact equality, all worth checking first when debugging.

Example: Common Bug Categories to Watch For

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	int arr[3] = {1, 2, 3};
	// Loop: repeat while the condition holds
	for (int i = 0; i < 3; i++) {
		// Print to the console with cout
		std::cout << arr[i] << " ";
	}
	// Print to the console with cout
	std::cout << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Leaving debug print statements in the finished program.
  2. Ignoring compiler warnings, which often point to real bugs.
  3. Relying on assert() for real error handling, when it is disabled with NDEBUG.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.