Share via


strtof, _strtof_l, wcstof, _wcstof_l

 

The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.

The latest version of this topic can be found at strtof, _strtof_l, wcstof, _wcstof_l.

Converts strings to a single-precision floating-point value.

Syntax

float strtof(  
   const char *nptr,  
   char **endptr   
);  
float _strtof_l(  
   const char *nptr,  
   char **endptr,  
   _locale_t locale  
);  
float wcstof(  
   const wchar_t *nptr,  
   wchar_t **endptr   
);  
float wcstof_l(  
   const wchar_t *nptr,  
   wchar_t **endptr,  
   _locale_t locale  
);  

Parameters

nptr
Null-terminated string to convert.

endptr
Pointer to the character that stops the scan.

locale
The locale to use.

Return Value

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

wcstof returns values analogously to strtof. 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.

For more information about return codes, see errno, _doserrno, _sys_errlist, and _sys_nerr.

Remarks

Each function converts the input string nptr to a float. The strtof function converts nptr to a single-precision value. strtof stops reading the string nptr at the first character it cannot recognize as part of a number. This may be the terminating null character. wcstof is a wide-character version of strtof; its nptr argument is a wide-character string. Otherwise, these functions behave identically.

Generic-Text Routine Mappings

TCHAR.H routine _UNICODE & _MBCS not defined _MBCS defined _UNICODE defined
_tcstof strtof strtof wcstof
_tcstof_l _strtof_l _strtof_l _wcstof_l

The LC_NUMERIC category setting of the current locale determines recognition of the radix character in nptr; for more information, see setlocale, _wsetlocale. The functions that don't have the _l suffix use the current locale; the ones that have the suffix are identical except that they use the locale that's 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 that's 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 that's pointed to by endptr.

strtof 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.

Requirements

Routine Required header
strtof, _strtof_l C: <stdlib.h> C++: <cstdlib> or <stdlib.h>
wcstof, _wcstof_l C: <stdlib.h> or <wchar.h> C++: <cstdlib>, <stdlib.h> or <wchar.h>

For additional compatibility information, see Compatibility.

Example

// crt_strtof.c  
// This program uses strtof to convert a  
// string to a single-precision value.  
  
#include <stdlib.h>  
#include <stdio.h>  
  
int main( void )  
{  
   char *string;  
   char *stopstring;  
   float x;  
  
   string = "3.14159This stopped it";  
   x = strtof(string, &stopstring);  
   printf("string = %s\n", string);  
   printf("   strtof = %f\n", x);  
   printf("   Stopped scan at: %s\n\n", stopstring);  
}  
string = 3.14159This stopped it  
   strtof = 3.141590  
   Stopped scan at: This stopped it  

See Also

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