Convert a string to double (atof and _wtof), integer (atoi, _atoi64, _wtoi and _wtoi64), or long integer (atol and _wtol).
double atof(
const char *string
);
double _wtof(
const wchar_t *string
);
int atoi(
const char *string
);
__int64 _atoi64(
const char *string
);
int _wtoi(
const wchar_t *string
);
__int64 _wtoi64(
const wchar_t *string
);
long atol(
const char *string
);
long _wtol(
const wchar_t *string
);
Parameters
- string
- String to be converted.
Return Value
Each function returns the double, int, __int64, or long value produced by interpreting the input characters as a number. The return value is 0 (for atoi, _atoi64, _wtoi, and _wtoi64), 0L (for atol and _wtol), or 0.0 (for atof and _wtof) if the input cannot be converted to a value of that type. The return value is undefined in case of overflow.
Remarks
These functions convert a character string to a double-precision, floating-point value (atof and _wtof), an integer value (atoi, _atoi64, _wtoi and _wtoi64), or a long integer value (atol and _wtol).
The input string is a sequence of characters that can be interpreted as a numerical value of the specified type. The output value is affected by the setting of the LC_NUMERIC category in the current locale. For more information on the LC_NUMERIC category, see setlocale. The function stops reading the input string at the first character that it cannot recognize as part of a number. This character may be the null character ('\0' or L'\0') terminating the string.
The string argument to atof and _wtof has the following form:
[whitespace] [sign] [digits] [.digits] [ {d | D | e | E }[sign]digits]
A whitespace consists of space or 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 decimal point, at least one must appear after the decimal point. The decimal digits may be followed by an exponent, which consists of an introductory letter (d, D, e, or E) and an optionally signed decimal integer.
atoi, _atoi64, atol, _wtoi, _wtoi64 and _wtol do not recognize decimal points or exponents. The string argument for these functions has the form:
[whitespace] [sign]digits
where whitespace, sign, and digits are as described for atof and _wtof.
Generic-Text Routine Mappings
| TCHAR.H routine | _UNICODE & _MBCS not defined | _MBCS defined | _UNICODE defined |
| _tstof | atof | atof | _wtof |
| _tstoi | atoi | atoi | _wtoi |
| _tstoi64 | _atoi64 | _atoi64 | _wtoi64 |
| _tstol | atol | atol | _wtol |
| _ttoi | atoi | atoi | _wtoi |
| _ttoi64 | _atoi64 | _atoi64 | _wtoi64 |
| _ttol | atol | atol | _wtol |
Requirements
| Routine | Required header | Compatibility |
| atof | <math.h> and <stdlib.h> | ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP |
| atoi | <stdlib.h> | ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP |
| _atoi64 | <stdlib.h> | Win 98, Win Me, Win NT, Win 2000, Win XP |
| atol | <stdlib.h> | ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP |
| _wtof | <stdlib.h> or <wchar.h> | Win 98, Win Me, Win NT, Win 2000, Win XP |
| _wtoi | <stdlib.h> or <wchar.h> | Win 98, Win Me, Win NT, Win 2000, Win XP |
| _wtoi64 | <stdlib.h> or <wchar.h> | Win 98, Win Me, Win NT, Win 2000, Win XP |
| _wtol | <stdlib.h> or <wchar.h> | Win 98, Win Me, Win NT, Win 2000, Win XP |
For additional compatibility information, see Compatibility in the Introduction.
Libraries
All versions of the C run-time libraries.
Example
This program shows how numbers stored as strings can be converted to numeric values using the atof, atoi, and atol functions.
// crt_atof.c
#include <stdlib.h>
#include <stdio.h>
int main( void )
{
char *s; double x; int i; long l;
s = " -2309.12E-15"; /* Test of atof */
x = atof( s );
printf( "atof test: \"%s\"; float: %e\n", s, x );
s = "7.8912654773d210"; /* Test of atof */
x = atof( s );
printf( "atof test: \"%s\"; float: %e\n", s, x );
s = " -9885 pigs"; /* Test of atoi */
i = atoi( s );
printf( "atoi test: \"%s\"; integer: %d\n", s, i );
s = "98854 dollars"; /* Test of atol */
l = atol( s );
printf( "atol test: \"%s\"; long: %ld\n", s, l );
}
Output
atof test: " -2309.12E-15"; float: -2.309120e-012
atof test: "7.8912654773d210"; float: 7.891265e+210
atoi test: " -9885 pigs"; integer: -9885
atol test: "98854 dollars"; long: 98854
See Also
Data Conversion Routines | Floating-Point Support Routines | Locale Routines | _ecvt | _fcvt, _gcvt | setlocale, strtod | wcstol | strtoul | Run-Time Routines and .NET Framework Equivalents