Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Library Function - strtod()

C Library Function - strtod()

C Standard Library - <stdlib.h> C Standard Library -

Description

C Library Function **double strtod(const char *str, char endptr) Converts the string pointed to by the argument str converts the string pointed to by endptr to a floating-point number (of type double). If

Declaration

Below is the declaration of the strtod() function.



double strtod(const char *str, char **endptr)

Parameters

Return Value

This function returns the converted double-precision floating-point number; if no valid conversion was performed, it returns zero (0.0).

Example

The following example demonstrates the use of the strtod() function.



#include 

#include 



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's compile and run the above program, which will produce the following result:



数字(double)是 20.303000

字符串部分是 | This is test|

C Standard Library - <stdlib.h> C Standard Library -