| Functions | |||||||||||||||||||||||||||||||||||||||||
| Character manipulation | |||||||||||||||||||||||||||||||||||||||||
| Conversions to and from numeric formats | |||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||
| String manipulation | |||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||
| String examination | |||||||||||||||||||||||||||||||||||||||||
| Memory manipulation | |||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||
| Miscellaneous | |||||||||||||||||||||||||||||||||||||||||
(C11)(C11) | |||||||||||||||||||||||||||||||||||||||||
Defined in header <stdlib.h> | ||
double atof(constchar* str); | ||
Interprets a floating-point value in a byte string pointed to bystr.
Function discards any whitespace characters (as determined byisspace) until first non-whitespace character is found. Then it takes as many characters as possible to form a valid floating-point representation and converts them to a floating-point value. The valid floating-point value can be one of the following:
e orE followed with optional minus or plus sign and nonempty sequence of decimal digits (defines exponent to base10)
| (since C99) |
Contents |
| str | - | pointer to the null-terminated byte string to be interpreted |
double value corresponding to the contents ofstr on success. If the converted value falls out of range of the return type, the return value is undefined. If no conversion can be performed,0.0 is returned.
The name stands for “ASCII to float”.
#include <stdio.h>#include <stdlib.h> int main(void){printf("%g\n", atof(" -0.0000000123junk"));printf("%g\n", atof("0.012"));printf("%g\n", atof("15e16"));printf("%g\n", atof("-0x1afp-2"));printf("%g\n", atof("inF"));printf("%g\n", atof("Nan"));printf("%g\n", atof("1.0e+309"));// UB: out of range of doubleprintf("%g\n", atof("0.0"));printf("%g\n", atof("junk"));// no conversion can be performed}
Possible output:
-1.23e-080.0121.5e+17-107.75infnaninf00
(C99)(C99) | converts a byte string to a floating-point value (function)[edit] |
C++ documentation foratof | |