C Data Type Functions - C strtold






Convert string to long double

Prototype

long double strtold (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 already allocated object of 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 long double. 

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

If the correct value is out of the range of representable values for the type, a positive or negative HUGE_VALL is returned, and errno is set to ERANGE.

Example


#include <stdio.h> /* printf, NULL */
#include <stdlib.h> /* strtold */
/*from   ww  w  . ja va  2s . com*/
int main ()
{
  char szOrbits[] = "12313.123 123.24";
  char * pEnd;
  long double f1, f2;
  f1 = strtold (szOrbits, &pEnd);
  f2 = strtold (pEnd, NULL);
  printf ("Pluto takes %.2Lf years to complete an orbit.\n", f1/f2);
  return 0;
} 

       

The code above generates the following result.