fegetexceptflag() function in C/C++17 Mar 2025 | 4 min read 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:
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.
By knowing the state of exceptions, the program can take precise actions based on the exception that occurred during the floating-point arithmetic operation.
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. Example:Let us take a C++ program to illustrate the fegetexceptflag() function: Output: ![]() Explanation: 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. Example:Let us take a C program to illustrate the fegetexceptflag() function: Output: ![]() Explanation: 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. Next Topicforward_list::emplace_front() in C++ |
In this article, we will discuss an In-Place Algorithm for string transformation in C++ with several examples. In this algorithm, transfer all items with even positions for a given string to the end of the string. Maintain the same relative order for all elements that are placed...
9 min read
The stoi is a C++ standard library function that converts a string to an integer. It stands for "string to integer". It takes a string as input and returns the corresponding integer value. The function can raise an exception of type std::invalid_argument if the input string...
2 min read
In this article, we will discuss the with its syntax, parameters, and examples. What is the static_assert? The static_assert is a built-in feature in C++. It allows us to assert the statements during compile time. It is introduced in the C++11 version. This feature contains two main...
4 min read
In this article, we will discuss how to count of substrings with the frequency of at most one character as Odd in C++ with different approaches. Character subsets or sequences that are contiguous within a string are called substrings. It is now necessary to ascertain the number of...
4 min read
We can swap two numbers without using third variable. There are two common ways to swap two numbers without using third variable: By + and - By * and / Program 1: Using * and / Let's see a simple C++ example to swap two numbers without using third...
1 min read
Introduction A computational tool called "" was put together to determine the centered icosahedral numbers from user-defined input. The vertices of an icosahedron, a polyhedron with twenty equilateral triangular faces, are the beginning of the sequence formed by these numbers. Centered icosahedral numbers are important in mathematics...
5 min read
In C and C++, character arithmetic involves arithmetic operations using characters and symbols. Characters are like numbers beneath, even though they are typically used for text. It implies that there are intriguing ways to deal with characters and to add and subtract from them. In this...
3 min read
C++ Generic Programming Introduction Using C++ templates, the generic programming pattern generalizes the approach so that it may be used with a variety of data types. Instead of specifying the actual data type, we supply a placeholder in templates, and that placeholder is substituted by the data...
7 min read
In this article, you will learn about the rules for operator overloading in C++. There are several rules for operator overloading in C++. Some main rules are as follows: 1. Syntax Overloading an operator is defined by defining a function with the operator keyword followed by the Operator...
3 min read
String::npos is a static member constant of the std::string class in C++. It stands for the largest std::string object that can be created. When utilizing string-related actions, such as locating substrings or characters, this value is frequently used to signify the lack of a valid position...
4 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India