I have forgotten
my Password

Or login with:

  • Facebookhttp://facebook.com/
  • Googlehttps://www.google.com/accounts/o8/id
  • Yahoohttps://me.yahoo.com
ComputingCStdlib.h

atoi_l

Convert ASCII string to integer
+ View other versions (4)

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

#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\n",str,n);
 
  char *str2 = "314hello";
  n = atoi(str2);
  printf("The string %s as an integer is = %d\n",str2,n);

return 0; }
Output:
The string 123456 as an integer is = 123456
The string 314hello as an integer is = 314

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

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

DOSUNIXWindowsANSI CC++ only
atoi••••