What is feholdexcept in C ?

The feholdexcept function saves the present floating point environment. It then sets the present values of floating point status flags again.

Prototype

The function prototype is as follows:

int feholdexcept( fenv_t* env )

Parameters

env - pointer to object of type fenv_t. It contains the present status of the floating point environment.

Return value

Figure 1: Return values of the function under different conditions.

Example

The code below shows the use of the feholdexcept function. The function is used to prevent exceptions in case of overflow and division by zero, as shown in line 12:

#include <stdio.h>
#include <float.h>
#include <fenv.h>
#pragma STDC FENV_ACCESS ON
void show_fe_excepts(void)
{
printf("exceptions raised: ");
if(fetestexcept(FE_DIVBYZERO)) printf(" FE_DIVBYZERO");
if(fetestexcept(FE_INVALID)) printf(" FE_INVALID");
if(fetestexcept(FE_ALL_EXCEPT)==0) printf(" none");
printf("\n");
}
double func (double x)
{
fenv_t curr_excts;
// Save and clear current f-p environment.
feholdexcept(&curr_excts);
// Raise inexact exceptions.
printf("In func(): x = %f\n", x = x/0);
show_fe_excepts();
// clear exception
feclearexcept(FE_DIVBYZERO);
// calling the function
feupdateenv(&curr_excts);
return x;
}
int main(void)
{
show_fe_excepts();
printf("func(DBL_MAX) = %f\n", func(DBL_MAX));
show_fe_excepts();
return 0;
}

Free Resources

Copyright ©2026 Educative, Inc. All rights reserved