0% found this document useful (0 votes)
3 views6 pages

Debugging and Testing

The document discusses debugging and testing in C programming, outlining common types of errors: compilation, runtime, and logical errors, along with examples and coding tasks for each. It also highlights debugging techniques and tools, particularly Visual Studio Code, for identifying and fixing issues in code. Additionally, it covers writing test cases and the importance of unit testing in validating individual components of software applications.

Uploaded by

annshu75
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

Debugging and Testing

The document discusses debugging and testing in C programming, outlining common types of errors: compilation, runtime, and logical errors, along with examples and coding tasks for each. It also highlights debugging techniques and tools, particularly Visual Studio Code, for identifying and fixing issues in code. Additionally, it covers writing test cases and the importance of unit testing in validating individual components of software applications.

Uploaded by

annshu75
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Debugging and Testing

Common debugging practices:


What is Debugging?
Debugging is the process of finding and fixing errors in your code. Think of it as being a
detective, searching for clues to solve the mystery of why your program isn't working as
expected.
Types of Errors
In C programming, you'll commonly encounter three types of errors:
1. Compilation Errors (Syntax Errors)
2. Runtime Errors
3. Logical Errors (Wrong Answer)

Let's explore each of these in detail.

Compilation Errors
Compilation errors, also known as syntax errors, occur when your code doesn't follow the
rules of the C language.
Example:
#include <stdio.h>

int main() {
int number = 19;
printf("%d\n", number)
}

If you run this code, you'll get an error message like this:

sol.c: In function 'main':


sol.c:5:25: error: expected ';' before '}' token
5 | printf("%d\n", number)
| ^
| ;
6|}
|~
The error message tells us that there's a problem on line 5, character 25. It's expecting a
semicolon (;) before the closing curly brace (}).

To fix this, we simply need to add a semicolon at the end of line 5:

printf("%d\n", number);
Coding Task:
Fix the following code to correctly output the number 15:
#include <stdio.h>

int main() {
int number = 15
printf("%d\n" number)
}
Runtime Errors
Runtime errors occur when your code has correct syntax but encounters a problem during
execution.
Example:
One common runtime error is division by zero:
int a = 5;
int b = 0;
int c = a / b;
printf("%d", c);

This code will compile successfully but will crash when you try to run it because division by
zero is undefined.

Coding Task:
Modify the following code to handle division by zero:
#include <stdio.h>

int main() {
int a, b;
scanf("%d %d", &a, &b);
printf("%d", a / b);
return 0;
}
Hint: Use an if statement to check if b is zero, and print "infinity" in that case.

Logical Errors
Logical errors, also known as semantic errors, are the trickiest to spot. Your code will
compile and run without any error messages, but it won't produce the expected output.
Example:
int n = 10;
if (n % 2 == 0) {
printf("odd");
} else {
printf("even");
}

This code will compile and run, but it will incorrectly print "odd" for even numbers and
"even" for odd numbers.

Common Types of Logical Errors:


1. Incorrect Conditions: Using the wrong comparison operators or logical operators in if-
else statements.
2. Incorrect Indexing: Forgetting that arrays in C are zero-indexed, leading to off-by-one
errors.
3. Incorrect Variable Usage: Using the wrong variable in a calculation or output statement.
Coding Task:
Fix the logical error in this code:
#include <stdio.h>

int main() {
int num;
scanf("%d", &num);

if (num < 5) {
printf("Greater than 5");
} else {
printf("Less than or equal to 5");
}

return 0;
}

Common Debugging Techniques


1. Read the Error Message: Compiler errors often point directly to the line and character
where the problem occurs.
2. Use Print Statements: Add printf() statements to check variable values at different
points in your code.
3. Check Your Logic: Step through your code mentally or on paper to ensure it does what
you intend.
4. Simplify and Isolate: If you have a complex program, try to isolate the problem by
commenting out parts of the code or creating a simpler version that reproduces the issue.
5. Use a Debugger: Learn to use a debugger tool, which allows you to step through your
code line by line and inspect variables.

Use of debugging tools:


When software does not work as expected, computer programmers study the code to
determine why any errors occurred. They use debugging tools to run the software in a
controlled environment, check the code step by step, and analyze and fix the issue.

1. Visual Studio Code (VSCode)


Visual Studio is the IDE of choice for software practitioners across multiple
disciplines. The built-in debugging functionality supports engineers developing in
[Link], PHP, Go, C#, and countless other languages due to its extensible support
for most modern language choices.

When you run VSCode in debugger mode (Ctrl+Shift+D), you can set breakpoints
and use watches to observe the values contained in variables. You can also step
through each line of your code to understand how each line interacts with these
variables and the path taken with each execution of the code.

You can learn more about the debugger in the VSCode Docs . The documentation
applies specifically to the built-in [Link] debugger, but many extensions offer
similar functionality within your chosen language.

Writng testcases:
In the previous problem, each testcase had 2 lines of input - each consisting of integers.
Test cases can also contain a combination of integers and strings.

Task
Let's write a program in the IDE which performs the following:

The 1st line of input contains t - the count of testcases.


Each testcase consists of the following 2 lines of input:
The 1st line of the testcase contains 2 integers - accept them as variables A and B.
The 2nd line of the testcase contains 1 string - accept it as a variable S.
For each test case, output on a single line the 2 integers followed by the string.
#include <stdio.h>

int main() {
int t;
int A, B;
char C[30];
int i = 1;
scanf("%d", &t);
while (i <= t) {
scanf("%d %d", &A, &B);
scanf("%s", &C);
printf("%d %d %s ", A, B, C);
i = i + 1;
}
}

Sample 1:
Input
Output
2
12
abcde
34 567
A1B2C3
1 2 abcde
34 567 A1B2C3
Explanation:
Test case 1:
12
abcde

Output for test case 1: 1 2 abcde

Test case 2:
34 567
A1B2C3

Output for test case 2: 34 567 A1B2C3

Unit Testing

Unit testing involves the testing of each unit or an individual component of the software
application. It is the first level of functional testing. The aim behind unit testing is to validate
unit components with its performance.

A unit is a single testable part of a software system and tested during the development phase
of the application software.

The purpose of unit testing is to test the correctness of isolated code. A unit component is an
individual function or code of the application. White box testing approach used for unit
testing and usually done by the developers.

Whenever the application is ready and given to the Test engineer, he/she will start checking
every component of the module or module of the application independently or one by one,
and this process is known as Unit testing or components testing.

You might also like