Syntax
#include <stdlib.h> long double strtold(const char *nptr, char **endptr);Description
strtold converts a character string pointed to by nptr to a long double value. When it reads a character it does not recognize as part of a number, strtold stops conversion and endptr points to the remainder of nptr. This character may be the ending null character.
The string pointed to by nptr must have the following format: ┌──────────────────────────────────────────────────────────────────────────────┐
│ │
│ >>──┬────────────┬──┬────────────────────────────────────────┬──>
│
│ └─whitespace─┘ └─┬─────┬──┬─digits──┬───┬──┬────────┬─┬─┘ │
│ ├─ + ─┤ │ └─.─┘ └─digits─┘ │ │
│ └─ ┴ ─┘ └─.──digits─────────────────┘ │
│ │
│ >──┬──────────────────────────┬──>< │
│ └─┬─ e ─┬──┬─────┬──digits─┘ │
│ └─ E ─┘ ├─ + ─┤ │
│ └─ ┴ ─┘ │
│ │
└──────────────────────────────────────────────────────────────────────────────┘
The digits are one or more decimal digits. If no digits appear before the decimal point, at least one digit must follow the decimal point. An exponent expressed as a decimal integer can follow the digits. The exponent can be signed.
The value of nptr can also be one of the strings infinity, inf, or nan. These strings are case-insensitive, and can be preceded by a unary minus (-). They are converted to infinity and NaN values. See Infinity and NaN Support for more information about using infinity and NaN values.
If the string pointed to by nptr does not have the expected form, no conversion is performed and endptr points to the value of nptr.
The strtold function ignores any white-space characters, as defined by the isspace function.
If successful, strtold returns the value of the long double number. If it fails, strtold returns 0. For an underflow or overflow, it returns the following:
Condition
This example uses strtold to convert two strings, " -001234.5678e10end of string" and "NaNQthis cannot be converted" to their corresponding long double values. It also prints out the part of the string that cannot be converted.
#include <stdlib.h>#include <stdio.h> int main(void) { char *nptr; char *endptr; nptr = " -001234.5678e10end of string"; printf("strtold = %.10Le\n", strtold(nptr, &endptr)); printf("end pointer at = %s\n\n", endptr); nptr = "NaNthis cannot be converted"; printf("strtold = %.10Le\n", strtold(nptr, &endptr)); printf("end pointer at = %s\n\n", endptr); return 0; /**************************************************************************** The output should be: strtold = -1.2345678000e+13 end pointer at = end of string strtold = nan end pointer at = this cannot be converted ****************************************************************************/ }Related Information