Easy Tutorial
❮ C Error Handling C Function Calloc ❯

C Library Function - strtod()

C Standard Library - <stdlib.h>

Description

The C library function double strtod(const char str, char *endptr) converts the string pointed to by the argument str to a floating-point number (type double). If endptr is not NULL, a pointer to the character after the last character used in the conversion is stored in the location referenced by endptr.

Declaration

Here is the declaration for the strtod() function.

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

Parameters

Return Value

The function returns the converted double-precision floating-point number, and if no valid conversion could be performed, it returns zero (0.0).

Example

The following example demonstrates the use 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("The number (double) is %lf\n", ret);
  printf("The string part is |%s|", ptr);

  return(0);
}

Let's compile and run the above program, which will produce the following result:

The number (double) is 20.303000
The string part is | This is test|

C Standard Library - <stdlib.h>

❮ C Error Handling C Function Calloc ❯