6.19.9 atof Function
Converts a string to a double precision floating-point value.
Include
<stdlib.h>
Prototype
double atof(const char *s);
Argument
s
- pointer to the string to be converted
Return Value
Returns the converted value if successful; otherwise, returns 0.
Remarks
The number may consist of the following:
[whitespace] [sign] digits [.digits] [ {
e | E }[sign]digits]
Optional whitespace followed by an optional sign, then a sequence of one or more digits
with an optional decimal point, followed by one or more optional digits and an optional
e
or E
followed by an optional signed exponent.
The conversion stops when the first unrecognized character is reached. The conversion is
the same as strtod(s,0)
except it does no error checking so
errno
will not be set.
Example
See the notes at the beginning of this chapter or section for
information on using printf()
or scanf()
(and other functions reading and writing the stdin
or
stdout
streams) in the example code.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char a[] = " 1.28";
char b[] = "27.835e2";
char c[] = "Number1";
double x;
x = atof(a);
printf("String = \"%s\" float = %f\n", a, x);
x = atof(b);
printf("String = \"%s\" float = %f\n", b, x);
x = atof(c);
printf("String = \"%s\" float = %f\n", c, x);
}
Example Output
String = "1.28" float = 1.280000
String = "27.835e2" float = 2783.500000
String = "Number1" float = 0.000000