C Program to Handle Divide by Zero
Last Updated :
16 Apr, 2025
In C programming, there is no built-in exception handling like in other high-level languages such as C++, Java, or Python. However, you can still handle exceptions using error checking, function return values, or by using signal handlers.
There are 2 methods to handle divide-by-zero exception mentioned below:
- Manually Checking before Division
- Signal handling
Manually Checking before Division
The most used method is to check if the divisor (number that divides the dividend) in the division is zero or not using if else statement.
Example:
C
#include <stdio.h>
#include <float.h>
int main() {
float a = 10, b = 0;
float res;
// Check division by zero
if(b == 0){
printf("Error: Division by zero");
}else{
res = a / b;
printf("%f", res);
}
return 0;
}
OutputError: Division by zero
Explanation: The program checks for division by zero using if (b == 0) before performing the division. If b is 0, it prints an error message.
Using Signal Handling
Signal handling can be used to catch runtime exceptions like divide-by-zero errors. In case of floating-point errors, SIGFPE (Floating Point Exception) is raised. We can create a signal handler function and assign it to SIGFPE using signal() function. The setjump and longjmp can be used to jump to the previous valid state of the program, allowing you to handle the exception and resume execution. The SIGFPE can be cleared by using the feclearexcept and fetestexcept functions from the fenv.h header
Example:
C
#include <fenv.h>
#include <setjmp.h>
#include <signal.h>
#include <stdio.h>
#include <float.h>
jmp_buf recovery;
void handle_divide_by_zero(int sig) {
// Re-assign the signal handler
signal(SIGFPE, handle_divide_by_zero);
printf("Error: Division by zero\n");
// Jump to the recovery point
longjmp(recovery, 1);
}
int main() {
double a = 10, b = 0, res;
int recovery_status;
// Assign the signal handler
signal(SIGFPE, handle_divide_by_zero);
// Set a recovery point
recovery_status = setjmp(recovery);
if (recovery_status == 0) {
res = a / b;
if(fetestexcept(FE_DIVBYZERO)) {
feclearexcept(FE_DIVBYZERO);
raise(SIGFPE);
}
else {
printf("%f", res);
}
}
return 0;
}
Explanation: Program executes step by step as follow:
- The program sets up a signal handler for SIGFPE to catch floating-point exceptions like division by zero.
- It attempts to divide a by b, where b is 0, which will cause a floating-point exception.
- If the division by zero exception occurs and raise(SIGFPE) will trigger the signal handler (handle_divide_by_zero()).
- The signal handler prints an error message and then uses longjmp() to return to the recovery point, avoiding the program crashing.
- The program can then proceed without further errors (or cleanup operations), as it has recovered from the division by zero.
Similar Reads
C Program to Make a Simple Calculator A simple calculator is a program that can perform addition, subtraction, multiplication, and division of two numbers provided as input. In this article, we will learn to create a simple calculator program in C.ExampleInput: a = 10, b = 5, op = +Output: 15.00Explanation: Chosen operation is addition,
3 min read
C Program to Compute Quotient and Remainder Given two numbers A and B. The task is to write a program to find the quotient and remainder of these two numbers when A is divided by B. Examples: Input: A = 2, B = 6 Output: Quotient = 0, Remainder = 2 Input: A = 17, B = 5 Output: Quotient = 3, Remainder = 21. Using Division and Modulo Operator In
7 min read
How to Take Operator as Input in C? In C, an operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. We may need to take operator as an input in some cases. In this article, we will learn how to take an operator as input in C. Get Input Operator in CTo take an operator as input in C, we
2 min read
remquo() Function in C The remquo() function in C is part of the standard math library <math.h> and is used to compute the remainder and part of the quotient of the division of two floating-point numbers. This function is particularly useful when we need both the remainder and some information about the quotient sim
3 min read
How to Deal with Divide by Zero Errors in R Programming Divide-by-zero errors are a common issue encountered in programming, including in R Programming Language. These errors can disrupt calculations and lead to incorrect results or program crashes. This article will explore the causes of divide-by-zero errors in R and provide methods to handle and preve
4 min read