atoi (表示 ascii to integer)是把字符串轉換成整型數的一個函數,應用在計算機程序和辦公軟件中。
int atoi(const char *nptr) 函數會掃描參數 nptr字符串,會跳過前面的空白字符(例如空格,tab縮進)等。
如果 nptr不能轉換成 int 或者 nptr爲空字符串,那麼將返回 0 。特別注意,該函數要求被轉換的字符串是按十進制數理解的。atoi輸入的字符串對應數字存在大小限制(與int類型大小有關),若其過大可能報錯-1。
例如:
//vs2013裏調用printf函數請使用預處理命令#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int n;
char *str = "12345.67";
n = atoi(str);
printf("n=%d\n",n);
return 0;
}
輸出:
n = 12345