What is nanf() in C/C++?

The nanf() function is used to get the NaN (not a number) value of the type float.

It accepts an argument that is basically a string, then returns the NaN value of type float.

Standard library

#include<cmath>

Syntax

float nanf (const char* str);

Parameters

str - a string value

Return value

It returns a NaN value which is in float.

Code

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
float nanValue;
//generating generic NaN value by passing an empty string
nanValue = nanf("");
cout << "nanValue: " << nanValue << endl;
return 0;
}

Free Resources

Copyright ©2026 Educative, Inc. All rights reserved