Practical 15: Error Handling in C
Note: This is Practical 15 in this notes collection, not a verified official NU experiment number. Follow your department's current lab manual if its sequence or requirements differ.
1. Aim
To understand common types of errors in C and write programs that validate input, check operation results, and handle basic runtime error conditions.
2. Learning Outcomes
After completing this practical, you should be able to:
- Identify syntax, runtime, and logical errors.
- Use compiler diagnostics to locate errors.
- Validate input using the return value of
scanf(). - Prevent common invalid operations such as integer division by zero.
- Check the return value of file operations.
- Use return codes to report success or failure.
3. Theory
3.1 What Is an Error?
An error is a problem in a program that prevents it from compiling, causes it to behave unexpectedly, or makes it produce an incorrect result.
3.2 Common Types of Errors
| Error type | Meaning | Example |
|---|---|---|
| Syntax error | The code violates C language grammar | Missing semicolon |
| Runtime error | A problem occurs while the program is running | Trying to use a resource that could not be opened |
| Logical error | The program runs but produces an incorrect result | Using addition instead of multiplication |
A compiler can detect many syntax errors and some other issues, but it cannot identify every logical mistake.
3.3 Input Validation
Input validation means checking whether the input received by a program is usable before processing it.
For example:
int number;
if (scanf("%d", &number) != 1)
{
printf("Invalid input.\n");
return 1;
}
For a single integer conversion, scanf() should return 1 when the conversion succeeds.
3.4 Return Values
Many C functions report success or failure through their return values.
Examples:
scanf()returns the number of input items successfully assigned.fopen()returns a file pointer on success andNULLon failure.fclose()returns zero on success andEOFif an error occurs while closing.- A program commonly returns
0frommain()to indicate successful completion and a non-zero value to indicate failure.
3.5 Error Messages
A useful error message should explain what went wrong and, when possible, how the user can correct it.
For example:
printf("Invalid input. Please enter an integer.\n");
Avoid displaying a success message when an operation has failed.
4. Program 1: Validate Integer Input
Aim
To check whether the user has entered a valid integer.
Algorithm
- Start.
- Declare an integer variable.
- Ask the user to enter an integer.
- Check the return value of
scanf(). - If the conversion fails, display an error message and return a non-zero status.
- Otherwise, display the entered number.
- Stop.
C Program
#include <stdio.h>
#include <limits.h>
int main(void)
{
int number;
printf("Enter an integer: ");
if (scanf("%d", &number) != 1)
{
printf("Invalid input. Please enter an integer.\n");
return 1;
}
printf("You entered: %d\n", number);
return 0;
}
Sample Output 1: Valid Input
Enter an integer: 25
You entered: 25
Sample Output 2: Invalid Input
Enter an integer: hello
Invalid input. Please enter an integer.
Explanation
scanf("%d", &number)attempts to read an integer.- If the input cannot be converted, the return value is not
1. - The program displays an error and returns
1to indicate failure.
5. Program 2: Handle Division by Zero
Aim
To prevent integer division by zero by checking the divisor before performing division.
Algorithm
- Start.
- Read two integers.
- Check whether input was successful.
- Check whether the second integer is zero.
- If it is zero, display an error message.
- Otherwise, perform integer division and display the result.
- Stop.
C Program
#include <stdio.h>
#include <limits.h>
int main(void)
{
int numerator, denominator;
printf("Enter numerator and denominator: ");
if (scanf("%d %d", &numerator, &denominator) != 2)
{
printf("Invalid input. Enter two integers.\n");
return 1;
}
if (denominator == 0)
{
printf("Error: Division by zero is not allowed.\n");
return 1;
}
if (numerator == INT_MIN && denominator == -1)
{
printf("Error: The quotient is outside the int range.\n");
return 1;
}
printf("Result = %d\n", numerator / denominator);
return 0;
}
Sample Output 1: Valid Division
Enter numerator and denominator: 20 4
Result = 5
Sample Output 2: Zero Denominator
Enter numerator and denominator: 20 0
Error: Division by zero is not allowed.
Explanation
The program checks denominator == 0 before evaluating numerator / denominator. This avoids performing an invalid integer division.
6. Program 3: Check Whether a File Opens Successfully
Aim
To handle a file-opening failure using the return value of fopen().
Algorithm
- Start.
- Attempt to open a file in read mode.
- Check whether the returned pointer is
NULL. - If it is
NULL, display an error message and stop with a non-zero status. - Otherwise, display a success message.
- Close the file.
- Stop.
C Program
#include <stdio.h>
int main(void)
{
FILE *fp = fopen("input.txt", "r");
if (fp == NULL)
{
printf("Error: Could not open input.txt for reading.\n");
return 1;
}
printf("File opened successfully.\n");
if (fclose(fp) == EOF)
{
printf("Error: Could not close the file properly.\n");
return 1;
}
return 0;
}
Sample Output 1: File Exists and Opens
File opened successfully.
Sample Output 2: File Does Not Exist or Cannot Be Opened
Error: Could not open input.txt for reading.
Explanation
- The file is opened in read mode, so it must already exist and be accessible.
fopen()returnsNULLif it cannot open the file.- The program checks the pointer before using it.
fclose()is also checked for a reported closing error.
7. Program 4: Check Input Before Calculating an Average
Aim
To validate the number of marks and prevent division by zero when calculating an average.
C Program
#include <stdio.h>
int main(void)
{
int count, i;
float mark;
double total = 0.0;
printf("How many marks? ");
if (scanf("%d", &count) != 1 || count <= 0 || count > 1000)
{
printf("Error: Enter a number of marks from 1 to 1000.\n");
return 1;
}
for (i = 1; i <= count; i++)
{
printf("Enter mark %d: ", i);
if (scanf("%f", &mark) != 1 || !(mark >= 0.0f && mark <= 100.0f))
{
printf("Error: Enter a valid mark from 0 to 100.\n");
return 1;
}
total += mark;
}
printf("Average = %.2f\n", total / count);
return 0;
}
Sample Output
How many marks? 3
Enter mark 1: 70
Enter mark 2: 80
Enter mark 3: 90
Average = 80.00
Explanation
- The program rejects a non-positive count.
- Each mark is checked before it is added.
- Since
countmust be greater than zero, the average calculation does not divide by zero.
8. Debugging: A Practical Approach
When a program does not work as expected, follow these steps:
- Read the compiler error message carefully.
- Check the reported line and nearby statements.
- Check variable declarations, data types, and format specifiers.
- Trace the program using sample input.
- Use temporary
printf()statements to inspect variable values. - Test normal, boundary, and invalid inputs.
- Correct one problem at a time and run the program again.
Example of a Logical Error
Incorrect code:
int length = 5;
int width = 3;
int area = length + width;
The code may compile and run, but it calculates the wrong result for the area of a rectangle.
Correct code:
int area = length * width;
This is a logical error because the formula is wrong, not the C syntax.
9. Common Mistakes
- Ignoring the return value of
scanf(). - Dividing by zero.
- Using a file pointer after
fopen()has returnedNULL. - Forgetting to close an opened file.
- Displaying a success message before checking whether an operation succeeded.
- Assuming every program that compiles will produce the correct answer.
- Treating a logical error as a compiler error.
- Testing only normal input and never checking invalid or boundary cases.
10. How to Compile and Run
If GCC is installed, save one program in a file such as error_handling.c, then run:
gcc error_handling.c -o error_handling
./error_handling
On Windows Command Prompt:
gcc error_handling.c -o error_handling.exe
error_handling.exe
Try both valid and invalid inputs to observe how the program responds.
11. Practice Tasks
- Write a program that accepts two integers and checks the input before adding them.
- Write a program that rejects a negative number before calculating its square root.
- Write a program that checks whether a file exists before reading it.
- Write a program that checks the return value of
fclose(). - Find and correct a logical error in a program that calculates the perimeter of a rectangle.
- Test a program using normal, zero, negative, and invalid inputs.
12. Viva Questions
- What is an error in a C program?
- What is a syntax error?
- What is a runtime error?
- What is a logical error?
- Can a program compile successfully and still produce a wrong answer?
- What does
scanf()return? - Why should input be validated?
- What does
fopen()return when it fails? - Why should a program check a file pointer before using it?
- What does a non-zero return value from
main()commonly indicate? - Why should division by zero be prevented?
- What is debugging?
- How can
printf()help locate a logical error? - Why should programs be tested with invalid and boundary inputs?
Practical status: Suggested learning exercise based on the Error Handling topic in the SPL theory outline stored in this repository. It is not presented as an official NU practical number.