Skip to main content

9. Common Debugging Tips

9.1 Compilation Errors

  1. Missing semicolons: Add ; at the end of statements
  2. Undeclared variables: Declare variables before using them
  3. Type mismatches: Ensure compatible types in assignments (this mostly happens in function parameters or calling, we will learn more about this on the next module)
  4. Missing headers: Include necessary header files

9.2 Runtime Errors

  1. Segmentation faults: Check array bounds and pointer usage.
    • this error is the hardest part when it comes to debugging because its highly related to a wrong memory allocation or pointer usage such as accessing an unaccessible variable or array index. We will learn more about this in the next module
  2. Infinite loops: Verify loop conditions and increment/decrement
  3. Wrong output: Check format specifiers in printf/scanf

9.3 Debugging Techniques

// Add debug prints to trace program execution
printf("Debug: x = %d, y = %d\n", x, y);

// Check intermediate results
int temp = a + b;
printf("Intermediate result: %d\n", temp);
int result = temp * c;