Computer Programming Lab 12
Computer Programming Lab 12
Lab 12
Objective:
Debugging Techniques in Programming
Student Information
Student Name
Student ID
Date
Assessment
Marks Obtained
Remarks
Signature
1
Lab 12: Debugging Techniques in Programming
Objective:
The main goal of this lab is to familiarize students with effective debugging techniques to identify
Debugging is a crucial aspect of the software development process in C++ as it plays a pivotal role in
ensuring the reliability and correctness of a program. Even the most seasoned developers encounter
errors in their code, and effective debugging is essential for identifying and rectifying these issues.
Debugging not only helps in eliminating errors but also enhances the overall quality of the code,
1. Syntax Errors:
- Syntax errors occur when the code violates the rules of the C++ language. These errors are
typically detected by the compiler during the compilation phase. Examples include missing
2. Runtime Errors:
- Runtime errors occur during the execution of the program. They often lead to program crashes or
unexpected behavior. Examples include division by zero, accessing an out-of-bounds array element,
3. Logic Errors:
- Logic errors are more subtle and challenging to detect. They occur when the program's logic is
flawed, leading to incorrect results. Examples include incorrect algorithm implementation, flawed
2
Key Debugging Tools in C++:
1. Print Statements:
- The use of print statements (e.g., `cout` statements) is a simple yet effective way to trace the flow
- Debugging environments, such as GDB (GNU Debugger), provide advanced debugging features.
These include setting breakpoints, stepping through code, and inspecting variable values interactively.
gdb ./your_program
(gdb) run
- Version control systems like Git allow developers to track changes in their code base. This is
valuable for identifying when errors were introduced and for collaborating with other developers.
a. Print Statements:
Using print statements, such as `cout` in C++, is a fundamental and straightforward method to
understand the flow of the program and inspect the values of variables at different points during
runtime.
Example:
#include <iostream>
3
int main() {
int x = 5;
return 0;
Exercise:
Given a C++ program with logical errors, use print statements strategically to identify and correct
the issues.
#include <iostream>
int main() {
int x = 5;
int y = 0;
y = x * i;
4
cout << "x: " << x << endl;
return 0;}
In this example:
1. Added print statements to display the values of variables (`i`, `x`, `y`) at different points in the
program.
By strategically placing print statements and analyzing the output, you can identify and correct logical
errors in your C++ program. Keep in mind that this is a simplified example, and in a real-world
scenario, you may encounter more complex issues that require a more in-depth debugging approach,
identifying and fixing errors more efficiently. Key features include setting breakpoints, stepping
Example:
Using breakpoints in an IDE like Visual Studio Code:
5
2. Start debugging the program.
3. Execution will pause at the breakpoint, allowing you to inspect variable values.
Exercise: Debug a C++ program using breakpoints and step-by-step execution in your
preferred IDE.
#include <iostream>
int main() {
int x = 5;
int y = 0;
y = x * i;
return 0;}
To debug a C++ program using breakpoints and step-by-step execution in Dev C++, follow these
steps:
2. Set a Breakpoint:
- Click on the left margin of the editor window at that line number. This sets a breakpoint.
- Ensure that you are compiling your program with debugging information. In Dev C++, this is
6
typically done by selecting a "Debug" build or ensuring that debugging information is included in
4. Start Debugging:
- The program will start, and execution will pause at the line where you set the breakpoint.
6. Inspect Variables:
- You can now inspect the values of variables by hovering over them or by using the Watches
window.
- Use the debugging toolbar or the keyboard shortcuts to step through the code:
- Step Into (F7): Executes the current line and moves to the next line. If the current line is a
- Step Over (F8): Executes the current line. If the current line is a function call, it executes the
entire function and moves to the next line in the calling function.
- Step Out (Shift + F8): Executes the remaining lines of the current function and returns to the
calling function.
8. Continue Execution:
- If you want to run the program until the next breakpoint, select "Continue" from the debugging
- The call stack window shows you the current function calls. You can use this to trace the flow of
your program.
- Use the Watch window to monitor the values of specific variables during execution.
7
11. Fix Issues:
- As you step through the code, you may identify the issues causing unexpected behavior. Fix them
- Once you've fixed the issues, stop debugging by selecting "Stop Debugging" from the "Run"
c. Logging:
Logging involves strategically placing log statements in the code to capture information about the
program's behavior during runtime. This can help track the flow of execution and identify unexpected
behavior.
Example:
#include <iostream>
#include <fstream>
logFile.close();}
int main() {
logMessage("Program started");
return 0;}
Exercise: Implement logging in a C++ program and analyze the log output to identify and
fix issues.
#include <iostream>
8
#include <fstream>
// Logging function
logfile.close();}
int main() {
// Initialize variables
int x = 5;
int y = 0;
logMessage("Attempting division...");
if (y != 0) {
int result = x / y;
} else {
return 0;}
9
Follow the steps below to compile and run this program in Dev C++:
- Ensure that you are compiling the program with debugging information enabled.
Attempting division...
By following these steps, you can implement logging in a C++ program using Dev C++, allowing
Task:
10
#include <iostream>
int main() {
int x = 5;
int y = 3;
int result = x * y; //
Incorrect
multiplication
cout << "Result: "
<< result << endl;
return 0;}
11