C Library Functions -strtod()
C Standard Library - <stdlib.h>
Description
C Library Functionsdouble strtod(const char *str, char **endptr)Pass the parameterstrconverts the string pointed to into a floating-point number (of type double). Ifendptris not null, a pointer to the character after the last character in the conversion is stored in the location referenced by endptr.
Declaration
The following is the declaration of the strtod() function.
double strtod(const char *str, char **endptr)
Parameter
- str-- the string to be converted to a double-precision floating-point number.
- endptr-- a reference to an object of type char*, whose value is set by the function tostrThe next character after the numeric value.
Return Value
The function returns the converted double-precision floating-point number, or zero (0.0) if no valid conversion was performed.
Example
The following example demonstrates the usage of the strtod() function.
#include <stdio.h>
#include <stdlib.h>
int main()
{
char str[30] = "20.30300 This is test";
char *ptr;
double ret;
ret = strtod(str, &ptr);
printf("数字(double)是 %lf\n", ret);
printf("字符串部分是 |%s|", ptr);
return(0);
}
Let us compile and run the above program, which will produce the following result:
数字(double)是 20.303000 字符串部分是 | This is test|other extensions