function
<cstdlib>

strtold

long double strtold (const char* str, char** endptr);
Convert string to long double
Parses the C string str interpreting its content as a floating point number (according to the current locale) and returns its value as a long double. If endptr is not a null pointer, the function also sets the value of endptr to point to the first character after the number.

This function operates like strtod to interpret the string, but produces numbers of type long double (see strtod for details on the interpretation process).

Parameters

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.
This parameter can also be a null pointer, in which case it is not used.

Return Value

On success, the function 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.
If the correct value would cause underflow, the function returns a value whose magnitude is no greater than the smallest normalized positive number (some library implementations may also set errno to ERANGE in this case).

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/* strtold example */
#include <stdio.h>      /* printf, NULL */
#include <stdlib.h>     /* strtold */

int main ()
{
  char szOrbits[] = "90613.305 365.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;
}

Output:
Pluto takes 248.09 years to complete an orbit.


Data races

The array pointed by str is accessed, and the pointer pointed by endptr is modified (if not null).

Exceptions (C++)

No-throw guarantee: this function never throws exceptions.

If str does not point to a valid C-string, or if endptr does not point to a valid pointer object, it causes undefined behavior.

See also