atof()

Converts a character string to a numeric value of type double.

  #include <stdlib.h>
  
  double atof(const char *nptr);    
Parameter

nptr

A pointer to the string to convert.

Remarks

The atof() function converts the character array pointed to by nptr to a floating point value of type double. Except for its behavior on error, this function is the equivalent of the call strtod(nptr, NULL);

This function sets the global variable errno to ERANGE if the converted value cannot be expressed as a floating point value of type double.

atof() returns a floating point value of type double.

Listing: Example of atof(), atoi(), atol() usage

#include <stdlib.h>

#include <stdio.h>

int main(void)

{

int i;

long int j;

float f;

static char si[] = "-493", sli[] = "63870";

static char sf[] = "1823.4034";

f = atof(sf);

i = atoi(si);

j = atol(sli);

printf("%f %d %ld\n", f, i, j);

return 0;

}

Output:

1823.403400 -493 63870