<fenv.h> is the C99 header for the floating-point environment: the status flags that record exceptions such as divide-by-zero, overflow, and inexact results, and the current rounding mode.
Use it to detect whether a computation raised an exception without crashing, and to switch between rounding to nearest, up, down, or toward zero. The settings apply to the calling thread.
Remember
#include <fenv.h>
feclearexcept(FE_ALL_EXCEPT); /* clear flags */
... floating-point work ...
fetestexcept(FE_DIVBYZERO); /* nonzero if flag set */
fegetround() -> current rounding mode
fesetround(FE_UPWARD) -> 0 on success
link with -lm on GCC and Clang
Use
Prefer
Detect divide by zero or overflow
fetestexcept()
Reset flags before a computation
feclearexcept()
Change rounding direction
fesetround()
Save and restore everything
fegetenv() / fesetenv()
Approach
How to Use fenv.h in C
Clear the flags, run the floating-point work, then test the flags you care about.
Add #include <fenv.h>.
Call feclearexcept(FE_ALL_EXCEPT) before the code you want to inspect.
Run the floating-point operations. Use volatile or runtime values so the compiler does not fold them away.
Test flags with fetestexcept(mask); the result is nonzero when any flag in the mask is set.
If you change the rounding mode, save fegetround() first and restore it afterwards.
Piece
Required?
Role
#include <fenv.h>
Yes
Declares the functions and FE_* macros
#pragma STDC FENV_ACCESS ON
Recommended
Tells the compiler you read flags or change modes (some compilers, such as GCC, ignore it)
-lm
Usually
Links the math library on GCC and Clang
Pseudocode
Pseudocode
feclearexcept(FE_ALL_EXCEPT)
result = compute()
if fetestexcept(FE_DIVBYZERO | FE_OVERFLOW):
report problem
saved = fegetround()
fesetround(FE_DOWNWARD)
...
fesetround(saved)
Reference
Cheat Sheet
Exception and rounding macros are defined only when the platform supports them.
See how a division sets a flag and how rint(2.5) changes with the rounding mode.
Calculation
feclearexcept(FE_ALL_EXCEPT) -> all flags off
x = 1.0 / 0.0 -> result +inf, FE_DIVBYZERO set
fetestexcept(FE_DIVBYZERO) -> nonzero
rint(2.5) under each rounding mode:
FE_TONEAREST -> 2 (ties go to even)
FE_UPWARD -> 3
FE_DOWNWARD -> 2
FE_TOWARDZERO -> 2
Code
C Examples
Compile with gcc file.c -std=c11 -o out (add -lm at the end).
Example 1 — Detect a Divide by Zero
Divide by zero in floating point, then check the flag instead of crashing.
C
#include <fenv.h>
#include <math.h>
#include <stdio.h>
#pragma STDC FENV_ACCESS ON
int main(void) {
volatile double zero = 0.0;
volatile double result;
feclearexcept(FE_ALL_EXCEPT);
result = 1.0 / zero;
if (fetestexcept(FE_DIVBYZERO)) {
printf("divide by zero flagged\n");
}
printf("result is infinite: %s\n", isinf(result) ? "yes" : "no");
return 0;
}
Output
divide by zero flagged
result is infinite: yes
How It Works
Clear first.feclearexcept(FE_ALL_EXCEPT) removes old flags.
Prevent folding.volatile forces the division to happen at run time.
Floating-point does not trap.1.0 / 0.0 produces infinity and sets FE_DIVBYZERO.
Test the flag.fetestexcept returns nonzero, and isinf confirms the result.
Example 2 — Compare Rounding Modes
Round the same value with rint() under each rounding direction.
Clear before each test. Flags stay set until you clear them.
1/3 cannot be exact. The binary result is rounded, so FE_INEXACT is raised.
0.5 + 0.25 is exact. Both values are powers of two, so 0.75 is represented exactly.
Edge Cases & Pitfalls
Floating-point flags are easy to misread when the compiler optimizes aggressively.
pragma
Tell the compiler you read flags
Without #pragma STDC FENV_ACCESS ON (or an option such as -frounding-math on GCC) the compiler may reorder or fold operations across your flag tests.
const
Constant folding hides flags
1.0 / 0.0 written with literals can be folded at compile time, so the flag never sets at run time. Use volatile operands in demos.
restore
Restore the rounding mode
Save fegetround() and restore it before calling code that expects round-to-nearest.
optional
Macros are optional
FE_UPWARD and similar macros exist only when the hardware supports them, and fesetround returns nonzero for an unsupported mode.
Analysis
Performance & Portability Notes
Approach
Cost
Best for
fetestexcept / feclearexcept
Read or clear status flags; cheap
Checking a block of math
fesetround
Slower than ordinary arithmetic on many CPUs
Occasional mode switches
Switching per iteration
Can dominate the loop cost
Avoid in hot loops
Integer division by zero is different: it is undefined behavior and often crashes. Floating-point division by zero is defined by IEEE 754 and only sets a flag.
Remember
Key Takeaways
Role: inspect floating-point exception flags and control rounding.
Flow:feclearexcept, run the math, then fetestexcept.
Compiler: use volatile operands or FENV_ACCESS so operations are not folded away.
Restore: put the rounding mode back with fesetround when you are done.
One line:fenv.h lets you ask “did that calculation overflow, underflow, or round?” and choose how results are rounded.
Frequently Asked Questions
fenv.h is the C99 header for the floating-point environment: exception status flags and the rounding mode.
Call feclearexcept(FE_ALL_EXCEPT), perform the division, then test fetestexcept(FE_DIVBYZERO).
No. IEEE 754 floating-point division by zero produces infinity and sets FE_DIVBYZERO. Integer division by zero is undefined and may crash.
FE_TONEAREST, FE_UPWARD, FE_DOWNWARD, and FE_TOWARDZERO, when the platform supports them. Use fesetround() to select one.
On GCC and Clang with glibc the fenv functions live in libm, so add -lm at the end of the link line.
It is standard, but not every compiler honors it. GCC ignores it, so use volatile operands or optimization flags such as -frounding-math.