UNIT 8: Software Testing and Debugging
Contents
Check for the following errors and debug
• syntax errors.
• logical errors.
• build errors.
• semantic errors.
• any other errors
Test software according to expected standards
Error is an illegal operation performed by the user which results in abnormal working of the
program. Programming errors often remain undetected until the program is compiled or executed.
Some of the errors inhibit the program from getting compiled or executed. Thus, errors should be
removed before compiling and executing. The most common errors can be broadly classified as
follows.
Syntax Errors
These are also referred to as compile-time errors. These errors have occurred when the rule of C
writing techniques or syntaxes has been broken. These types of errors are typically flagged by the
compiler prior to compilation.
These occur when code violates the grammar rules of the C language. The compiler identifies
these before the program ever runs.
• Common Causes: Missing semicolons (;), unmatched parentheses () or curly braces {},
and misspelled keywords (e.g., prntf instead of printf).
• Resolution: Read the compiler’s error message, which typically points to the exact line
and character where the rule was broken
Example 1: In the below program we are getting an error because of a missing semicolon at the
end of the output statement (printf()) called syntax error.
Output
Example 2: In this case, we are getting errors because of missing parenthesis before the output
statement and below the main(). This type of error is also called syntax error.
Output
===================================================
Logical errors
Even if the syntax and other factors are correct, we may not get the desired results due to logical
issues. These are referred to as logical errors. We sometimes put a semicolon after a loop, which
is syntactically correct but results in one blank loop. In that case, it will display the desired output.
The program builds and runs successfully but produces the wrong output because the underlying
logic is flawed.
• Common Causes: Using the assignment operator = instead of the equality operator == in
an if statement, or accidental infinite loops due to incorrect conditions.
• Resolution: Use print-statement debugging or step through the code with a debugger to
verify the flow of execution.
Example: In the below example, the for loop iterates 5 times but the output will be displayed only
one time due to the semicolon at the end of for loop. This kind of error is called a logical error.
Output
Geeks for Geeks
===================================================
Build Errors/ Runtime Errors
This type of error occurs while the program is running. Because this is not a compilation error, the
compilation will be completed successfully. These errors occur due to segmentation fault when a
number is divided by division operator or modulo division operator.
These occur while the program is executing, often leading to crashes or "abnormal termination".
• Common Causes: Division by zero, accessing array elements out-of-bounds (segmentation
fault), and memory leaks.
• Resolution: Use debugging tools like GDB and check function return values—especially
for system calls—to handle unexpected states gracefully.
Example: Let us consider an array of length 5 i.e. array[5], but during runtime, if we try to access
10 elements i.e array[10] then we get segmentation fault errors called runtime errors. Giving only
an array length of 5
Output
-621007737
But in output trying to access more than 5 i.e if we try to access array[10] during runtime then
the program will throw an error or will show an abnormal behavior and print any garbage value.
===================================================
Semantic Errors
When a sentence is syntactically correct but has no meaning, semantic errors occur. This is similar
to grammatical errors. If an expression is entered on the left side of the assignment operator, a
semantic error may occur.
These occur when code is syntactically correct but makes no sense to the compiler in context.
• Common Causes: Using uninitialized variables, incompatible type assignments (e.g.,
assigning a string to an integer), or passing the wrong number of arguments to a function.
• Resolution: Enable and address all Compiler Warnings (e.g., using -Wall in GCC), as they
often flag semantic issues before they become runtime bugs.
Example: Below is the C program to show semantic error.
Output
===================================================
Any other errors: Linker Errors
When the program is successfully compiled and attempting to link the different object files with
the main object file, errors will occur. When this error occurs, the executable is not generated.
This could be due to incorrect function prototyping, an incorrect header file, or other factors. If
main() is written as Main(), a linked error will be generated.
Linker errors arise after successful compilation when the Linker tool fails to combine object files
into an executable.
• Common Causes: Writing Main() instead of main(), calling a function that hasn't been
defined, or failing to include necessary library flags during the build process.
• Resolution: Ensure all function prototypes match their definitions and that all external
libraries are correctly linked in your build settings.
Example: Below is the C program to show the linker error.
Output
===================================================
Test software according to expected standards
Good Habits to Avoid Errors
• Always initialize your variables
• Use meaningful variable names
• Keep your code clean and use indentation to stay organized
• Keep functions short and focused
• Check if loops or conditions are running as expected
• Read error messages carefully - they often tell you exactly where the problem is