What it is: A process of executing a software program with the intent of finding whether it satisfies specified requirements or not.
Why it matters: Testing helps ensure that our code works as intended, catching potential issues before they reach end-users.
What it is: The process of locating and removing errors (bugs) from a computer program.
Why it matters: Debugging helps us find and fix the mistakes we make when writing code, making our programs run correctly.
To test your C programs, you can use standard input/output functions like printf()
and scanf()
, or create custom functions to validate user inputs.
Example:
#include <stdio.h>
int add_numbers(int a, int b) {
return a + b;
}
int main() {
int num1 = 5;
int num2 = 7;
int result = add_numbers(num1, num2);
printf("The sum of %d and %d is: %d\n", num1, num2, result);
return 0;
}
To debug a C program, you can use tools such as GDB (GNU Debugger) or Visual Studio Code's built-in debugger. Here's an example of using GDB:
#include <stdio.h>
int add_numbers(int a, int b) {
return a + b;
}
int main() {
int num1 = 5;
int num2 = 0; // Intentionally setting this to zero to trigger an error
int result = add_numbers(num1, num2);
printf("The sum of %d and %d is: %d\n", num1, num2, result);
return 0;
}
To debug this code using GDB, compile it first:
gcc -g main.c -o main
Then run the compiled program with GDB:
gdb ./main
(gdb) run
GDB will then pause execution at the point of error, allowing you to inspect variables and step through code to identify and fix the issue.
SIGSEGV
)What causes it: Attempting to access memory that has not been allocated or is outside the bounds of an array.
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
printf("%d\n", arr[6]); // Segmentation fault occurs here
return 0;
}
Error message:
Program received signal SIGSEGV: Segmentation fault - invalid memory reference.
Solution: Ensure that you're accessing memory within the proper bounds or using dynamic memory allocation functions such as malloc()
.
Why it happens: Accessing unallocated or out-of-bounds memory causes your program to read or write data where it shouldn't, which can lead to unexpected results and crashes.
How to prevent it: Use appropriate array indices, check bounds before accessing memory, and be mindful of dynamic memory allocation.
0
)What causes it: Dividing a number by zero.
#include <stdio.h>
int main() {
int num = 10;
double result = num / 0.0; // This line will cause a division by zero error
printf("The value of %d divided by 0 is: %f\n", num, result);
return 0;
}
Error message:
Floating point exception (core dumped)
Solution: Ensure that the denominator is never zero before performing division.
Why it happens: Division by zero results in an undefined or infinite value, which can cause problems for your program's calculations and lead to errors.
How to prevent it: Check if the denominator is zero before performing division and handle such cases appropriately (e.g., returning an error message).