nanf() Function with Example in C++

C++ nanf() function: Here, we are going to learn about the nanf() function with example of cmath header in C++ programming language.
Submitted by IncludeHelp, on May 20, 2020

C++ nanf() function

nanf() function is a library function of cmath header, it is used to get the NaN value of type float. It accepts an argument (which is an implementation-specific C String – to get NaN value we have to pass an empty string), it returns NaN value of type float.

Not-A-Number (NaN) values are used to check whether the value is an unidentified/non-representable or not? For example, the square root of a negative number is unidentified.

Syntax of nanf() function:


    float nanf (const char* tagp);


  • tagp – represents an implementation-specific C-String.

Return value:

The return type of this method is float, it returns NaN value.


    Function call:

C++ code to demonstrate the example of nanf() function

// C++ code to demonstrate the example of
// nanf() function

#include <iostream>
#include <cmath>
using namespace std;

// main() section
int main()
    float nanValue;

    //generating generic NaN value
    //by passing an empty string
    nanValue = nanf("");

    //printing the value
    cout << "nanValue: " << nanValue << endl;

    return 0;


nanValue: nan

Reference: C++ nanf() function

Comments and Discussions!

Load comments ↻

Copyright © 2024 All rights reserved.