C Data Type Functions - C strtod






Converts a string to a double.

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

The string str is converted to a floating-point number (type double).

Any initial whitespace characters are skipped.

The number may consist of an optional sign, a string of digits with an optional decimal character, and an optional e or E followed by a optionally signed exponent.

Conversion stops when the first unrecognized character is reached.

Prototype

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

Parameter

This function has the following parameter.

str
C-string beginning with the representation of a floating-point number.
endptr
Reference to an type char*, whose value is set by the function to the next character in str after the numerical value.

Return

returns the converted floating point number as a value of type double.

If no valid conversion could be performed, the function returns zero ( 0.0).





Example


#include <stdio.h> /* printf, NULL */
#include <stdlib.h> /* strtod */
/* ww  w  .ja va2  s .  c  om*/
int main (){
  char szOrbits[] = "123.24 123.3";
  char* pEnd;
  double d1, d2;
  d1 = strtod (szOrbits, &pEnd);
  d2 = strtod (pEnd, NULL);
  printf ("%.2f \n", d1/d2);
  return 0;
} 

       

The code above generates the following result.