6.19.34 strtold Function
Convert string to long double precision floating-point value.
Include
<stdlib.h>
Prototype
long double strtold(const char * restrict nptr, char ** restrict
endptr);
Argument
nptr
- the string to attempt to convert
endptr
- pointer to the remainder of the string that was not converted
Return Value
The converted value, or 0 if the conversion could not be performed.
Remarks
The strtold
function attempts to convert the first part
of the string pointed to by nptr
to a long double
floating-point value.
The initial section consists of any white-space characters.
- Decimal digits optionally
containing a decimal-point character, then an optional exponent part, being
e
orE
followed by an option sign and decimal digits - A
0x
or0X
, then a nonempty sequence of hexadecimal digits optionally containing a decimal-point character, then an optional binary exponent part, beingp
orP
, and an optional sign, and decimal digits. - one of
INF
orINFINITY
, ignoring case NAN
, ignoring case, optionally followed by any sequence contain digits or non-digits:
endptr
, provided that endptr
is not a null
pointer.The final section consists of characters in the string that were unrecognized in the
subject section and which will include the terminating null character of the string.
This section of the string will be accessible via the address stored to
endptr
, provided endptr
is not a null
pointer.
If the correct value of the conversion is outside the range of representable values,
the value of ERANGE
is stored in errno
and the
function returns the type-specific HUGE_VAL
value with the same
sign as the conversion. If the result underflows, the functions return a value whose
magnitude is no greater than the smallest normalized positive number in the return
type, but errno
is not set.
Example
See the notes at the beginning of this chapter or section for
information on using printf()
or scanf()
(and other functions reading and writing the stdin
or
stdout
streams) in the example code.
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
char * string = " +0.137e2 mSec";
char * final;
long double result;
result = strtold(string, &final);
printf("The floating-point conversion of the string \"%s\" is %Lg; final string part is \"%s\"\n", string, result, final);
}
Example Output
The floating-point conversion of the string " +0.137e2 mSec" is 13.7; final string part is " mSec"