atof, strtod, strtof, strtold - Converts a character
string to a double-precision floating-point value
const char *nptr ); double strtod(
const char *nptr,
char **endptr ); float strtof(
const char *nptr,
char **endptr ); long double strtold(
const char *nptr,
char **endptr );
Standard C Library (libc)
Points to the character string to convert. Specifies
either a null value, a pointer to the character that ended
the scan, or a pointer to a null value.
The atof() function converts, to a double floating-point
value, the string pointed to by the nptr parameter - up to
the first character that is inconsistent with the format
of a floating-point number. Leading space characters are
ignored. A call to this function is equivalent to a call
to strtod(nptr, (char **) NULL), except for error handling.
When the value cannot be represented, the result is
The strtod(), strtof(), and strtold() functions convert
the initial portion of the string pointed to by the nptr
parameter to double, float, and long double representation,
respectively. First, the input string is decomposed
into the following three parts: An initial, possibly
empty, sequence of space characters (as specified by the
isspace() function). A subject sequence interpreted as a
floating-point constant. A final string of one or more
unrecognized characters, including the terminating null
character of the input string.
After decomposition of the string, the subject sequence is
converted to a floating-point number and the resulting
value is returned. A subject sequence is defined as the
longest initial subsequence of the input string, starting
with the first non-space character, that is of the
expected form. The expected form and order of the subject
sequence is: An optional plus (+) or minus (-) sign. A
sequence of digits optionally containing a radix character.
An optional exponent part. An exponent part consists
of e or E, followed by an optional sign, which is followed
by one or more decimal digits.
The subject sequence contains no characters when the input
string is empty or consists entirely of space characters,
or when the first non-space character is other than a
sign, a digit, or a radix character.
For the strtod(), strtof(), and strtold() functions, when
the value of the endptr parameter is not (char**) NULL, a
pointer to the character that terminated the scan is
stored at *endptr.
When a floating-point value cannot be formed, *endptr is
set to nptr.
The strings NaN ("not a number"), Inf, and Infinity (the
case of the characters does not matter) are recognized as
valid only when the program is compiled with the -ieee
The setlocale() function may affect the radix character
used in the conversion result. Full use
When the string is empty or begins with an unrecognized
character, +0.0 is returned as the floating-point value.
If the calling routine is compiled with IEEE floating
point enabled (-ieee option), errno will be set to ERANGE
if the conversion underflows to zero. Similarly, if the
value overflows, ERANGE will be set and a properly signed
infinity will be returned.
If the calling routine is not compiled with IEEE floating
point enabled, any underflow will cause errno to be set to
ERANGE and a properly signed zero to be returned. An overflow
will cause errno to be set to ERANGE and will return
a properly signed DBL_MAX, FLOAT_MAX, or LDBL_MAX.
Upon successful completion, all of the functions return
the converted floating-point value.
If the atof(), strtod(), strtof(), or strtold() function
fails, errno may be set to the following value: The input
string is out of range (that is, the subject sequence cannot
be converted to a floating-point value without causing
underflow or overflow).
Functions: atoi(3), scanf(3)
[ Back ]