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

Ad: Are you a blogger? Join our Blogging forum.

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© some rights reserved.