What is feclearexcept() in C?

feclearexcept() clears all currently set floating-point exceptions and is defined in the fenv.h header.

Prototype

int feclearexcept(int excepts);

Parameters

  • except: a bitmask of type int corresponding to all the floating-point exceptions that need to be raised. The bitmask is a bitwise OR of the floating-point exceptions that have to be cleared.

Return value

  • If the argument is zero, or if all the floating-point exceptions have been cleared successfully, feclearexcept() returns 00. Otherwise, it returns a non-zero value.

Example

#include<stdio.h>
#include <math.h>
#include <fenv.h>
void raise_exceptions(){
float x = 1.0 / 0.0; //raises FE_DIVBYZERO
float y = sqrt(-1); //raises FE_INVALID
}
void display_raised_exceptions(){
if (fetestexcept(FE_DIVBYZERO)) printf("FE_DIVBYZERO RAISED!\n");
if (fetestexcept(FE_INVALID)) printf("FE_INVALID RAISED!\n");
if (fetestexcept(FE_INEXACT)) printf("FE_INEXACT RAISED!\n");
if (fetestexcept(FE_UNDERFLOW)) printf("FE_UNDERFLOW RAISED!\n");
if (fetestexcept(FE_OVERFLOW)) printf("FE_OVERFLOW RAISED!\n");
if (!fetestexcept(FE_ALL_EXCEPT)) printf("NO EXCEPTION RAISED!\n");
}
int main() {
raise_exceptions();
display_raised_exceptions();
printf("***CLEARING FE_DIVBYZERO EXCEPTION**\n");
feclearexcept(FE_DIVBYZERO);
display_raised_exceptions();
printf("***CLEARING ALL EXCEPTIONS**\n");
feclearexcept(FE_ALL_EXCEPT);
display_raised_exceptions();
return 0;
}

In the above code, we have two functions, raise_exceptions() and display_raised_exceptions().

First, we use the raise_exceptions() function to raise the FE_DIVBYZERO and FE_INVALID exceptions.

display_raised_exceptions() tests for any of the currently set exceptions and displays whether any of them are currently set or not.

After the call to raise_excpetions(), the FE_DIVBYZERO and FE_INVALID exceptions are raised. The subsequent call to feclearexcept(FE_DIVBYZERO) clears the FE_DIVBYZERO exception as shown in the output.

Finally, the call to feclearexcept(FE_ALL_EXCEPT) clears all floating-point exceptions.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved