Export (0) Print
Expand All
and
div
eof
not
or
xor
Expand Minimize

strtof, _strtof_l, wcstof, _wcstof_l

Visual Studio 2013

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

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
);

nptr

Null-terminated string to convert.

endptr

Pointer to the character that stops the scan.

locale

The locale to use.

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.

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] [ {d | D | 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 (d, D, 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.

Routine

Required header

strtof , _strtof_l

<stdlib.h>

wcstof , _wcstof_l

<stdlib.h> or <wchar.h>

For additional compatibility information, see Compatibility.

// 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
Show:
© 2014 Microsoft