Latest web development tutorials

C library functions - strtod ()

C standard library - <stdlib.h> C standard library - <stdlib.h>

description

C library functions to convert a stringdouble strtod (const char * str, char ** endptr) The argument strpoints to a floating-point number (of type double type). Ifendptr is not empty, then the pointer will point to the location to store the converted character after the last character in endptr references.

statement

Here is () statement strtod function.

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

parameter

  • str - a string to be converted to double-precision floating-point number.
  • A reference to an object of type char *, and its value is set by the function valuein str after the next character -endptr.

return value

This function returns the converted double-precision floating-point number, if there is no implementation of an effective conversion, it returns zero (0.0).

Examples

The following example demonstrates strtod () function is used.

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

数字(double)是 20.303000
字符串部分是 | This is test|

C standard library - <stdlib.h> C standard library - <stdlib.h>