Everything

strtodf


Conversion of character string to floating-point number (float type) (storing pointer in last character string)

[Classification]

Standard library

[Syntax]

#include <stdlib.h>

float strtodf(const char *str, char **ptr);

[Return value]

If the partial character string has been converted, the resultant value is returned. If the character string could not be converted, 0 is returned. If an overflow occurs (the value is not in the range in which it can be expressed), HUGE_VAL or -HUGE_VAL is returned, and ERANGE is set to global variable errno. If an underflow occurs, 0 is returned, and macro ERANGE is set to global variable errno.

[Description]

This function converts the first part of the character string indicated by str into a float type representation. The part of the character string to be converted is in the following format and is at the beginning of str with the maximum length, starting with a normal character that is not a space.

 

[ + | - ] digits [ . ] [ digits ] [ (e | E) [ + | - ] digits ]

 

If str is vacant or consists of space characters only, if the first normal character is other than "+", "-", ".", or a numeral, the partial character string does not include a character. If the partial character string is vacant, conversion is not executed, and the value of str is stored in the area indicated by ptr. If the partial character string is not vacant, it is converted, and a pointer to the last character string (including the null character (\0) indicating at least the end of str) is stored in the area indicated by ptr.

Remark

This function is not reentrancy.

[Example]

#include    <stdlib.h>
#include    <stdio.h>
void func(float ret) {
        char    *p, *str, s[30];
        str = "+5.32a4e";
        ret = strtodf(str, &p);         /*5.320000 is returned to ret, and pointer to 
                                          "a" is stored in area of p.*/
        sprintf(s, "%lf\t%c", ret, *p); /*"5.320000 a" is stored in array indicated 
                                          by s.*/
}