atoi
Convert ASCII 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
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.