atoi
convert <span class="Tn">ASCII</span> string to integer
INTERFACE
#include <stdlib.h> int atoi ( const char *nptr ) #include <xlocale.h> int atoi_l ( const char *nptr, locale_t loc )
DESCRIPTION
The atoi function converts the initial portion of the string pointed to by nptr to int representation. It is equivalent to:
(int)strtol(nptr, (char **)NULL, 10);The atoi function recognizes (in the following order)
- an optional string of tabs and spaces
- an optional sign
- a string of digits
While the atoi function uses the current locale, the atoi_l function may be passed a locale directly. See xlocale for more information.
Example 1
\code #include <stdlib.h> #include <stdio.h> int main(void) { char *str = "123456"; int n = atoi(str); printf("The string %s as an integer is = %d
",str,n); char *str2 = "314hello"; n = atoi(str2); printf("The string %s as an integer is = %d
",str2,n); return 0; } \endcode
Output:
The string 123456 as an integer is = 123456
The string 314hello as an integer is = 314IMPLEMENTATION NOTES
- The atoi function is not thread-safe and also not async-cancel safe.
- The atoi function has been deprecated by strtol and should not be used in new code.
ERRORS
The function atoi need not affect the value of errno on an error.
SEE ALSO
atof, atol, strtod, strtol, strtoul
STANDARDS
The atoi function conforms to ISO/IEC 9945-1:1990 ("POSIX.1"), ISO/IEC 9899:1990 ("ISO C90"), and ISO/IEC 9899:1999 ("ISO C99").
Compatibility
| DOS | UNIX | Windows | ANSI C | C++ only | |
|---|---|---|---|---|---|
| atoi | • | • | • | • |