RAD Studio |  |

strtod, _strtold, wcstod, _wcstold

Header File

stdlib.h

Category

Conversion Routines, Math Routines

Prototype

double strtod(const char *s, char **endptr);

double wcstod(const wchar_t *s, wchar_t **endptr);

long double _strtold(const char *s, char **endptr);

long double _wcstold(const wchar_t *s, wchar_t **endptr);

Description

Convert a string to a double or long double value.

strtod converts a character string, s, to a double value. s is a sequence of characters that can be interpreted as a double value; the characters must match this generic format:

[ws] [sn] [ddd] [.] [ddd] [fmt[sn]ddd]

where:

[ws] | = | optional whitespace
[sn] | = | optional sign (+ or -)
[ddd] | = | optional digits
[fmt] | = | optional e or E
[.] | = | optional decimal point

strtod also recognizes +INF and -INF for plus and minus infinity, and +NAN and -NAN for not-a-number.

For example, here are some character strings that strtod can convert to double:

+ 1231.1981 e-1

502.85E2

+ 2010.952

strtod stops reading the string at the first character that cannot be interpreted as an appropriate part of a double value.

If endptr is not null, strtod sets *endptr to point to the character that stopped the scan (*endptr = &stopper). endptr is useful for error detection.

_strtold is the long double version; it converts a string to a long double value.

Return Value

These functions return the value of s as a double (strtod) or a long double (_strtold). In case of overflow, they return plus or minus HUGE_VAL (strtod) or _LHUGE_VAL (_strtold).

Example

Copy Code

    #include <stdio.h>
    #include <stdlib.h>
    int main(void)
    {
       char input[80], *endptr;
       double value;
       printf("Enter a floating point number:");
       gets(input);
       value = strtod(input, &endptr);
       printf("The string is %s the number is %lf\n", input, value);
       return 0;
    }

Portability

| POSIX | Win32 | ANSI C | ANSI C++
strtod | + | + | + | +
_strtold |  | + |  |
wcstod |  | + | + | +
_wcstold |  | + |  |

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!