Description
Convert string to float
floating-point value.
Include
<stdlib.h>
Prototype
float strtod(const char * restrict nptr, char ** restrict
endptr);
Argument
nptr
endptr
Return Value
The converted value, or 0 if the conversion could not be performed.
Remarks
The strtof
function attempts to convert the first part
of the string pointed to by nptr
to a float
floating-point value.
Any initial whitespace characters in the string are skipped. The following characters represent the floating-point constant. Conversion stops once an unrecognized character is encountered in the string.
The expected form of the floating-point constant is an optional plus or minus sign, then one of the following:
e
or E
followed by an option sign and
decimal digits0x
or
0X
, then a nonempty sequence of hexadecimal digits
optionally containing a decimal-point character, then an optional binary
exponent part, being p
or P
, and option sign,
and decimal digits.INF
or
INFINITY
, ignoring caseNAN
, ignoring
case, optionally followed by any sequence contain digits or non-digits:Example
#include <stdlib.h> /* for strtof */
#include <stdio.h> /* for printf */
int main(void)
{
char * string = " +0.137e2 mSec";
char * final;
float result;
result = strtof(string, &final);
printf("The floating-point conversion of the string \"%s\" is %g; final string part is \"%s\"\n", string, result, final);
}
Example Output
The floating-point conversion of the string " +0.137e2 mSec" is 13.7; final string part is " mSec"