The first program of any length rarely works at the first attempt. This is normal, and it is as true for professionals as for beginners. What distinguishes a skilled programmer is a methodical way of finding and removing errors.
Errors and Debugging
An error in a program is commonly called a bug.
Definition: Debugging. The process of finding the cause of an error in a program and correcting it.
Debugging is different from testing. Testing shows that an error exists, by running the program with chosen data and comparing the output with the expected result. Debugging locates the cause and removes it.
Types of Errors
Errors fall into three classes, summarized in Table 4.13.
A syntax error breaks the grammar of the language: a missing semicolon, an unmatched brace or a misspelt keyword. The compiler detects it and reports the line, and no executable program is produced until it is corrected.
A runtime error occurs while the program is running. Dividing by zero and using an array index far out of range are examples. The program compiles but stops abnormally.
A logic error is a mistake in the method. The program compiles and runs to the end but gives a wrong answer. Using + where * was meant, or < where <= was needed, are logic errors. They are the hardest to find, because the computer gives no message.
Table 4.13: Types of errors in a C program
| Type | Detected by | When | Example |
|---|---|---|---|
| Syntax error | The compiler | During compilation | Missing semicolon |
| Runtime error | The system | While the program runs | Division by zero |
| Logic error | The programmer, by testing | From wrong output | avg = a + b / 2; |
A Systematic Method
Random changes rarely cure a bug and often add new ones. The following method, is reliable.
- Reproduce the error with definite input that always causes it.
- Locate the fault by tracing the values of the variables.
- Correct one cause at a time.
- Retest with the input that failed, and with related boundary cases, to confirm the cure and to check that nothing else was broken.
Techniques for Locating a Fault
- Reading compiler messages. The first message is the most reliable. A single mistake often causes several later messages, so the first error is corrected and the program compiled again.
- Hand tracing (dry run). The program is carried out on paper for a small input, recording each variable after every statement.
- Output statements. Temporary
printf()statements display the values of variables at chosen points, showing where they first go wrong. - Using a debugger. A debugger runs the program one statement at a time, stops at chosen lines called breakpoints, and shows the current values of variables.
- Simplifying the input. The smallest input that still shows the error is the easiest to trace.
Preventing Errors
Many bugs are avoided by habits already recommended in this course: planning the algorithm first, compiling after every small addition, using meaningful names and consistent indentation, initializing every variable, and testing the boundary cases.
Example 4.12: Finding a logic error
Problem: A program is meant to display the average of two marks. For the marks 40 and 60 it displays 70.00 instead of 50.00. Find and correct the error.
Solution:
- Reproduce: the input 40 and 60 always gives 70.00.
- Locate: the faulty statement is
avg = a + b / 2.0f;. Division has higher precedence than addition, so it computes 40 + 30 = 70. - Correct: add parentheses so that the sum is formed first.
- Retest with 40 and 60, as in the corrected program below.
#include <stdio.h>
int main(void)
{
int a = 40, b = 60;
float wrong, right;
wrong = a + b / 2.0f;
right = (a + b) / 2.0f;
printf("Without parentheses: %.2f\n", wrong);
printf("With parentheses: %.2f\n", right);
return 0;
}
Output:
Without parentheses: 70.00
With parentheses: 50.00
Result: The corrected expression gives 50.00. The compiler gave no message, because the original statement was valid C; only testing against a hand-calculated answer revealed the logic error.
Common mistakes
- Changing several things at once, so that it is impossible to tell which change mattered.
- Trying to correct every compiler message at once instead of starting with the first.
- Assuming that a program is correct because it compiles without errors.
Key takeaways
- Debugging finds and removes the cause of an error; testing reveals that an error exists.
- Errors are of three types: syntax, runtime and logic.
- The method is: reproduce, locate, correct one cause, retest.
- Tracing, output statements and a debugger are the main tools for locating a fault.
Practice questions
- Which type of error does the compiler detect?
- A program runs and displays a wrong answer. What type of error is this?
- Why should only one change be made at a time while debugging?
Answers to the practice questions
- Syntax errors.
- A logic error.
- So that the effect of each change is clear and the real cause of the error can be identified.
Sources
- Official BCA syllabus (NEP 2025 scheme), Problem Solving Techniques, course code 25BCA401SEC01, Unit IV
