C Program to Handle Divide by Zero Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report 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 DivisionSignal handlingManually Checking before DivisionThe 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 HandlingSignal 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 headerExample: 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; } OutputinfExplanation: 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. Comment More info S sanketnagare Follow Improve Article Tags : C Programs C Language Explore C BasicsC Language Introduction6 min readFeatures of C Programming Language3 min readC Programming Language Standard6 min readC Hello World Program1 min readCompiling a C Program: Behind the Scenes4 min readC Comments3 min readTokens in C4 min readKeywords in C2 min readVariables and ConstantsC Variables4 min readConstants in C4 min readConst Qualifier in C6 min readDifferent ways to declare variable as constant in C2 min readScope rules in C5 min readInternal Linkage and External Linkage in C4 min readGlobal Variables in C3 min readData TypesData Types in C5 min readLiterals in C4 min readEscape Sequence in C5 min readbool in C5 min readInteger Promotions in C2 min readCharacter Arithmetic in C2 min readType Conversion in C4 min readInput/OutputBasic Input and Output in C4 min readFormat Specifiers in C5 min readprintf in C5 min readscanf in C3 min readScansets in C2 min readFormatted and Unformatted Input/Output in C6 min readOperatorsOperators in C11 min readArithmetic Operators in C5 min readUnary Operators in C5 min readRelational Operators in C4 min readBitwise Operators in C6 min readC Logical Operators4 min readAssignment Operators in C4 min readIncrement and Decrement Operators in C4 min readConditional or Ternary Operator (?:) in C3 min readsizeof operator in C3 min readOperator Precedence and Associativity in C7 min readControl Statements Decision-MakingDecision Making in C (if , if..else, Nested if, if-else-if )7 min readC - if Statement4 min readC if else Statement3 min readC if , else if ladder4 min readSwitch Statement in C5 min readUsing Range in switch Case in C2 min readC - Loops6 min readC for Loop4 min readwhile Loop in C5 min readdo...while Loop in C4 min readFor vs. While4 min readContinue Statement in C4 min readBreak Statement in C5 min readgoto Statement in C4 min readFunctionsC Functions6 min readUser-Defined Function in C6 min readParameter Passing Techniques in C3 min readFunction Prototype in C4 min readHow can I return multiple values from a function?3 min readmain Function in C5 min readImplicit Return Type int in C2 min readCallbacks in C4 min readNested Functions in C4 min readVariadic Functions in C5 min read_Noreturn function specifier in C2 min readPredefined Identifier __func__ in C2 min readC Library math.h Functions6 min readArrays & StringsArrays in C6 min readProperties of Array in C7 min readMultidimensional Arrays in C - 2D and 3D Arrays8 min readInitialization of Multidimensional Array in C4 min readPass Array to Functions in C3 min readHow to pass a 2D array as a parameter in C?3 min readWhat are the data types for which it is not possible to create an array?2 min readHow to pass an array by value in C ?2 min readStrings in C6 min readArray of Strings in C3 min readWhat is the difference between single quoted and double quoted declaration of char array?2 min readC String Functions6 min readPointersC Pointers9 min readPointer Arithmetics in C with Examples10 min readC - Pointer to Pointer (Double Pointer)5 min readFunction Pointer in C6 min readHow to Declare a Pointer to a Function?2 min readPointer to an Array | Array Pointer5 min readDifference between constant pointer, pointers to constant, and constant pointers to constants3 min readPointer vs Array in C1 min readDangling, Void , Null and Wild Pointers in C6 min readNear, Far and Huge Pointers in C4 min readrestrict Keyword in C3 min readC User-Defined Data TypesC Structures9 min readdot (.) Operator in C2 min readC typedef3 min readStructure Member Alignment, Padding and Data Packing8 min readFlexible Array Members in a structure in C4 min readC Unions4 min readBit Fields in C8 min readDifference Between Structure and Union in C4 min readAnonymous Union and Structure in C3 min readEnumeration (or enum) in C5 min read Like