Javatpoint Logo
Javatpoint Logo

fegetexceptflag() function in C/C++

The fegetexceptflag function is part of the C standard library, specified explicitly in the <fenv.h> header. It is used for working with floating point exceptions in C programs. Floating point exceptions occur when certain arithmetic operations, such as overflow or invalid operations, result in exceptional conditions.

Syntax of the fegetexceptflag() function:

It has the following syntax:

int fegetexceptflag(fenv_t *envp, int excepts);

It takes two arguments that are envp and excepts

envp: It is a pointer to an object of the type fenv_t.

excepts: It is an integer bitmask specifying the floating-point exceptions to be tested. It is the combination of the following constants defined in <fenv.h> header

FE_DIVBYZERO: Divide-by-zero exception.

FE_INEXACT: Inexact (rounded) result exception.

FE_INVALID: Invalid operation exception.

FE_OVERFLOW: Overflow exception.

FE_UNDERFLOW: Underflow exception.

FE_ALL_EXCEPT: Bitmask representing all exceptions.

It returns zero on success and a non-zero value on failure.

The fenv_t type represents the entire floating-point environment, which includes control flags, status flags, and possibly other implementation-specific information related to floating-point operations.

Advantages of using fegetexceptflag:

There are several advantages of the fegetexceptflag. Some main advantages of the fegetexceptflag are as follows:

  • Control over floating-point exceptions:

It allows the program to query and control the state of the floating-point exceptions. It is used to handle exceptional conditions in a customized way.

  • Precision in Exception Handling:

By knowing the state of exceptions, the program can take precise actions based on the exception that occurred during the floating-point arithmetic operation.

  • Portability:

It provides a standardized way to work with floating point exceptions, making the code more portable across different systems and compilers that support the C standard.


Let us take a C++ program to illustrate the fegetexceptflag() function:


fegetexceptflag() function in C/C++


In this example, this program establishes a floating-point environment and checks for potential division by zero, issuing an error message if detected. After that, it attempts a division operation that might trigger the "Division by zero" exception and calculates the square root of a negative number, potentially causing an "Invalid operation" exception. The program captures the initial state of exception flags using fegetexceptflag and prints them in hexadecimal format. Specific checks for "Division by zero" and "Invalid operation" exceptions are performed, with corresponding messages displayed. Finally, the floating-point environment is reset before the program concludes.


Let us take a C program to illustrate the fegetexceptflag() function:


fegetexceptflag() function in C/C++


In this example, the program demonstrates the usage of the floating-point environment functions in the C standard library (<fenv.h>) to handle different floating-point exceptions. It sets up the current environment, performs various arithmetic operations that may trigger exceptions (such as division by zero, square root of a negative number, etc.), and checks for these exceptions using fegetexceptflag and fetestexcept. The program prints the initial state of all exceptions in hexadecimal format, identifies and prints specific exceptions that occurred, clears all exceptions with feclearexcept, and then prints the state of exceptions after clearing. Finally, it resets the floating-point environment to its original state using fesetenv.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA