C Data Type Functions - C strtold

Convert string to long double


long double strtold (const char* str, char** endptr);


This function has the following parameter.

C string beginning with the representation of a floating-point number.
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.


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.


#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.