C Standard Library

C <math.h> - nan() Function



The C <math.h> nan() function returns a quiet NaN (Not-A-Number) value of type double. The NaN values are used to identify undefined or non-representable values for floating-point elements, such as the square root of negative numbers or the result of 0/0. The argument can be used by library implementations to distinguish different NaN values in a implementation-specific manner.

Similarly, nanf and nanl return NaN values of type float and long double, respectively.

Syntax

double nan(const char* arg);
float nanf(const char* arg);
long double nanl(const char* arg);                         

Parameters

arg Specify an implementation-specific C-string identifying the contents of a NaN.

Return Value

Returns a quiet NaN value that corresponds to the identifying string arg or zero if the implementation does not support quiet NaNs.

Example:

The example below illustrates on nan() function.

#include <stdio.h>
#include <math.h>
 
int main (){
  float x = nanf("0/0");
  double y = nan("1");
  long double z = nanl("2");

  printf("nanf(0/0) = %f\n", x);
  printf("nan(\"1\") = %lf\n", y);
  printf("nanl(\"2\") = %Lf\n", z);  
  return 0;
}

The output of the above code will be:

nanf(0/0) = nan
nan("1") = nan
nanl("2") = nan

❮ C <math.h> Library