What is fegetexceptflag() in C?

fegetexceptflag() attempts to store all the currently raised exceptions included in the excepts in an object pointed to by the flag_p pointer. It is defined in the fenv.h header.

Prototype

int fegetexceptflag( fexcept_t* flagp, int excepts );

Parameters

  • flagp: A pointer of type fexcept_t that points to the object where the floating-point exceptions will be saved.
  • excepts: A bitmask consisting of the bitwise OR of all the floating-point exceptions that need to be saved.

Return value

  • fegetexceptflag() returns 00 on success; otherwise, it returns a non-zero value.

Example

#include<stdio.h>
#include <math.h>
#include <fenv.h>
void raise_exceptions(){
feraiseexcept(FE_OVERFLOW);
feraiseexcept(FE_INEXACT);
}
void display_raised_exceptions(){
printf("Currently raised exceptions are: ");
if (fetestexcept(FE_DIVBYZERO)) printf("FE_DIVBYZERO ");
if (fetestexcept(FE_INVALID)) printf("FE_INVALID ");
if (fetestexcept(FE_INEXACT)) printf("FE_INEXACT ");
if (fetestexcept(FE_UNDERFLOW)) printf("FE_UNDERFLOW ");
if (fetestexcept(FE_OVERFLOW)) printf("FE_OVERFLOW ");
if (!fetestexcept(FE_ALL_EXCEPT)) printf("NONE ");
printf("\n");
}
int main() {
fexcept_t excepts;
raise_exceptions();
display_raised_exceptions();
fegetexceptflag(&excepts, FE_ALL_EXCEPT);
printf("CLEARING ALL EXCEPTIONS...\n");
feclearexcept(FE_ALL_EXCEPT);
display_raised_exceptions();
printf("RESTORING STORED EXCEPTIONS...\n");
fesetexceptflag(&excepts, FE_ALL_EXCEPT);
display_raised_exceptions();
}

We initialize a variable of type fexcept_t called excepts, which we will use to store the floating-point exceptions.

In the function raise_exceptions(), we use feraiseexccept() to raise the floating-point exceptions of our choice.

We store these exceptions in the variable excepts and then clear all exceptions using:

feclearexcept(FE_ALL_EXCEPT);

Then, we restore the exceptions using the fesetexceptflag() function. The output will show that all of the exceptions that were stored in excepts have been raised.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved