strtod, _strtod_l, wcstod, _wcstod_l


The latest version of this topic can be found at strtod, _strtod_l, wcstod, _wcstod_l.

Convert strings to a double-precision value.


double strtod(  
   const char *nptr,  
   char **endptr   
double _strtod_l(  
   const char *nptr,  
   char **endptr,  
   _locale_t locale  
double wcstod(  
   const wchar_t *nptr,  
   wchar_t **endptr   
double wcstod_l(  
   const wchar_t *nptr,  
   wchar_t **endptr,  
   _locale_t locale  


Null-terminated string to convert.

Pointer to character that stops scan.

The locale to use.

Return Value

strtod returns the value of the floating-point number, except when the representation would cause an overflow, in which case the function returns +/–HUGE_VAL. The sign of HUGE_VAL matches the sign of the value that cannot be represented. strtod returns 0 if no conversion can be performed or an underflow occurs.

wcstod returns values analogously to strtod. For both functions, errno is set to ERANGE if overflow or underflow occurs and the invalid parameter handler is invoked, as described in Parameter Validation.

See _doserrno, errno, _sys_errlist, and _sys_nerr for more information on this and other return codes.


Each function converts the input string nptr to a double. The strtod function converts nptr to a double-precision value. strtod stops reading the string nptr at the first character it cannot recognize as part of a number. This may be the terminating null character. wcstod is a wide-character version of strtod; its nptr argument is a wide-character string. These functions behave identically otherwise.

Generic-Text Routine Mappings

TCHAR.H routine _UNICODE & _MBCS not defined _MBCS defined _UNICODE defined
_tcstod strtod strtod wcstod
_tcstod_l _strtod_l _strtod_l _wcstod_l

The LC_NUMERIC category setting of the current locale determines recognition of the radix character in nptr; for more information, see setlocale. The functions without the _l suffix use the current locale; _strtod_l is identical to _strtod_l except that they use the locale passed in instead. For more information, see Locale.

If endptr is not NULL, a pointer to the character that stopped the scan is stored at the location pointed to by endptr. If no conversion can be performed (no valid digits were found or an invalid base was specified), the value of nptr is stored at the location pointed to by endptr.

strtod expects nptr to point to a string of the following form:

[whitespace] [sign] [digits] [.digits] [ {e | E}[sign]digits]

A whitespace may consist of space and tab characters, which are ignored; sign is either plus (+) or minus (); and digits are one or more decimal digits. If no digits appear before the radix character, at least one must appear after the radix character. The decimal digits can be followed by an exponent, which consists of an introductory letter (e or E) and an optionally signed integer. If neither an exponent part nor a radix character appears, a radix character is assumed to follow the last digit in the string. The first character that does not fit this form stops the scan.

The UCRT versions of these functions do not support conversion of Fortran-style (d or D) exponent letters. This non-standard extension was supported by earlier versions of the CRT, and may be a breaking change for your code.


Routine Required header
strtod, _strtod_l C: <stdlib.h> C++: <cstdlib> or <stdlib.h>
wcstod, _wcstod_l C: <stdlib.h> or <wchar.h> C++: <cstdlib>, <stdlib.h> or <wchar.h>

For additional compatibility information, see Compatibility.


// crt_strtod.c  
// This program uses strtod to convert a  
// string to a double-precision value; strtol to  
// convert a string to long integer values; and strtoul  
// to convert a string to unsigned long-integer values.  
#include <stdlib.h>  
#include <stdio.h>  
int main( void )  
   char   *string, *stopstring;  
   double x;  
   long   l;  
   int    base;  
   unsigned long ul;  
   string = "3.1415926This stopped it";  
   x = strtod( string, &stopstring );  
   printf( "string = %s\n", string );  
   printf("   strtod = %f\n", x );  
   printf("   Stopped scan at: %s\n\n", stopstring );  
   string = "-10110134932This stopped it";  
   l = strtol( string, &stopstring, 10 );  
   printf( "string = %s\n", string );  
   printf("   strtol = %ld\n", l );  
   printf("   Stopped scan at: %s\n\n", stopstring );  
   string = "10110134932";  
   printf( "string = %s\n", string );  
   // Convert string using base 2, 4, and 8:  
   for( base = 2; base <= 8; base *= 2 )  
      // Convert the string:  
      ul = strtoul( string, &stopstring, base );  
      printf( "   strtol = %ld (base %d)\n", ul, base );  
      printf( "   Stopped scan at: %s\n", stopstring );  
string = 3.1415926This stopped it  
   strtod = 3.141593  
   Stopped scan at: This stopped it  
string = -10110134932This stopped it  
   strtol = -2147483648  
   Stopped scan at: This stopped it  
string = 10110134932  
   strtol = 45 (base 2)  
   Stopped scan at: 34932  
   strtol = 4423 (base 4)  
   Stopped scan at: 4932  
   strtol = 2134108 (base 8)  
   Stopped scan at: 932  

See Also

Data Conversion
Floating-Point Support
Interpretation of Multibyte-Character Sequences
String to Numeric Value Functions
strtol, wcstol, _strtol_l, _wcstol_l
strtoul, _strtoul_l, wcstoul, _wcstoul_l
atof, _atof_l, _wtof, _wtof_l
_create_locale, _wcreate_locale