strtof Function

Description

Convert string to float floating-point value.

Include

<stdlib.h>

Prototype

float strtod(const char * restrict nptr, char ** restrict endptr);

Argument

nptr
the string to attempt to convert
endptr
pointer to the remainder of the string that was not converted

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:

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"