Strtod

From aldeid
Jump to navigation Jump to search

Syntax

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

Description

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.

Parameters

str
This is the value to be converted to a string.
endptr
This is the reference to an already allocated object of type char*, whose value is set by the function to the next character in str after the numerical value.

Return Value

This function returns the converted floating point number as a double value, else zero value (0.0) is returned.

Example

Source Run
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
  char pi[100] = {0};

  printf("What is the value of PI? ");
  fgets(pi, 100, stdin);
  printf("Wow all that! Let's consider PI is equal to: %.2f.\n", strtod(pi, NULL));

  return 0;
}
$ ./code
What is the value of PI? 3.14159265359
Wow all that! Let's consider PI is equal to: 3.14.