Syntax
#include <stdlib.h> double atof(const char *string);Description
atof converts a character string to a double-precision floating-point value.
The input string is a sequence of characters that can be interpreted as a numerical value of the specified return type. The function stops reading the input string at the first character that it cannot recognize as part of a number; this character can be the null character that ends the string.
atof expects a string in the following form:
┌──────────────────────────────────────────────────────────────────────────────┐
│                                        │
│ >>──┬────────────┬──┬───┬──┬───────────────────────────┬──> 
       │
│   └─whitespace─┘  ├─+─┤  ├─digits──┬───┬──┬────────┬─┤           │
│           └─┴─┘  │     └─.─┘  └─digits─┘ │           │
│               └─.──digits─────────────────┘           │
│                                        │
│ >──┬──────────────────────┬──><                        │
│   └─┬─e─┬──┬───┬──digits─┘                          │
│    └─E─┘  ├─+─┤                               │
│       └─┴─┘                               │
│                                        │
└──────────────────────────────────────────────────────────────────────────────┘
The actual decimal point character (radix character) is determined by the LC_NUMERIC category of the current locale.
The white space consists of the same characters for which the isspace function is true, such as spaces and tabs. atof ignores leading white-space characters.
For atof, digits is one or more decimal digits; if no digits appear before the decimal point, at least one digit must appear after the decimal point. The decimal digits can precede an exponent, introduced by the letter e or E. The exponent is a decimal integer, which may be signed.
atof will not fail if a character other than a digit follows an E or if e is read in as an exponent. For example, 100elf will be converted to the floating-point value 100.0. The accuracy is up to 17 significant character digits. The string can also be "infinity", "inf", or "nan". These strings are case-insensitive, and can be preceded by a unary minus (-). They are converted to infinity and NaN values.
atof returns a double value produced by interpreting the input characters as a number. The return value is 0 if the function cannot convert the input to a value of that type. The return value is undefined in case of overflow.
This example shows how to convert numbers stored as strings to numerical values using the atof function.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
   double x;
   char *s;
   s = " -2309.12E-15";
   x = atof(s);                                           /* x = -2309.12E-15 */
   printf("atof( %s ) = %G\n", s, x);
   return 0;
   /****************************************************************************
      The output should be:
      atof(  -2309.12E-15 ) = -2.30912E-12
   ****************************************************************************/
}
Related Information