strtod()

Converts a character array to a floating point value of type double.

  #include <stdlib.h>
  
  double strtod(const char *nptr, char **endptr);    
Parameter

nptr

A pointer to a null-terminated character string to convert.

endptr

A pointer to a character pointer or a null pointer.

Remarks

The strtod() function converts a character array, pointed to by nptr, to a floating point value of type double. The character array can be in either decimal or hexadecimal floating point constant notation (examples: 103.578, 1.03578e+02, or 0x1.9efef9p+6).

If the endptr argument is not a null pointer, then strtod() assigns a pointer to a position within the character array pointed to by nptr. This position marks the first character that is not convertible to a value of type double.

In other than the "C" locale, additional locale-specific subject sequence forms may be accepted.

This function skips leading white space.

strtod() returns a floating point value of type double. If nptr cannot be converted to an expressible double value, strtod() returns HUGE_VAL, defined in math.h, and sets errno to ERANGE.

Listing: Example of strtod() and strtold() usage

#include <stdlib.h>

#include <stdio.h>

int main(void)

{

double f;

long double lf;

static char sf1[] = "103.578 777";

static char sf2[] = "1.03578e+02 777";

static char sf3[] = "0x1.9efef9p+6777";

char *endptr;

f = strtod(sf1, &endptr);

printf("Value = %f remainder of string = |%s|\n", f, endptr);

f = strtod(sf2, &endptr);

printf("Value = %f remainder of string = |%s|\n", f, endptr);

f = strtod(sf3, &endptr);

printf("Value = %f remainder of string = |%s|\n", f, endptr);

lf = strtold(sf1, &endptr);

printf("Value = %lf remainder of string = |%s|\n", lf, endptr);

lf = strtold(sf2, &endptr);

printf("Value = %lf remainder of string = |%s|\n", lf, endptr);

lf = strtold(sf3, &endptr);

printf("Value = %lf remainder of string = |%s|\n", lf, endptr);

return 0;

}

Output:

Value = 103.578000 remainder of string = |777|

Value = 103.578000 remainder of string = | 777|

Value = 103.748997 remainder of string = | 777|

Value = 103.578000 remainder of string = | 777|

Value = 103.578000 remainder of string = | 777|

Value = 103.748997 remainder of string = | 777|