NoneAtof() is the C runtime library function for converting the ASCII representation of a number to a floating point double.

Use this tag on for all questions about the use of atof() or where it does not seem to be working correctly.

Closely related are:

  • atoi for functions converting text to int, long, or long long
  • strtol for converting to long from text in any base from 2 through 36. Or choose a base automatically as the C compiler does depending on how the number is written. strtoul() converts to an unsigned long.
  • strtod for double
  • scanf for converting one or more values at a time directed by a format specification

SYNPOSIS

#include <stdlib.h>

double atof(const char *nptr);

atof() returns a value of zero if there is any problem during the conversion, though the documentation says that it does not detect errors. Since converting a zero also returns zero, there is no easy way of distinguishing an error from a correct conversion of zero. Use either scanf() or strtod() if error checking is needed.

atof() accepts integers, fixed decimal notation, and scientific notation. Some implementations also recognize the strings INF or INFINITY (disregarding case), NAN (disregarding case) optionally followed by (, a sequence of characters, followed by ). The character string specifies an implementation-dependent type of NAN. (Documentation of these strings are scant.)

Some implementations support hexadecimal formatted floats. These begin with 0x or 0X, followed by at least one hex digit, an optional decimal point (locale-specific), more digits, an optional p or P followed by the binary exponent in hex.