C examples for stdlib.h:strtof
function
<cstdlib> <stdlib.h>
Convert string to float and returns its value as a float.
float strtof (const char* str, char** endptr);
Parameter | Description |
---|---|
str | C-string representation of a floating-point number. |
endptr | char* to the next character in str after the numerical value. This parameter can be a null pointer if it is not used. |
On success, the function returns the converted floating point number as a value of type float.
For non valid conversion, the function returns zero 0.0F.
For out of the range values, a positive or negative HUGE_VALF is returned, and errno is set to ERANGE.
#include <stdio.h> #include <stdlib.h> int main ()/*from w w w. j a v a 2 s. com*/ { char str[] = "1234.97 6543.24 test"; char* pEnd; float f1, f2; f1 = strtof (str, &pEnd); f2 = strtof (pEnd, NULL); printf ("%f\n", f1); printf ("%f\n", f2); return 0; }