複製鏈接
請複製以下鏈接發送給好友

atoi

鎖定
atoi (表示 ascii to integer)是把字符串轉換成整型數的一個函數,應用在計算機程序辦公軟件中。int atoi(const char *nptr) 函數會掃描參數 nptr字符串,會跳過前面的空白字符(例如空格,tab縮進)等。如果 nptr不能轉換成 int 或者 nptr為空字符串,那麼將返回 0 [1]  。特別注意,該函數要求被轉換的字符串是按十進制數理解的。atoi輸入的字符串對應數字存在大小限制(與int類型大小有關),若其過大可能報錯-1。
外文名
atoi
參    數
字符串
返回值
int
適用語言
C/C++
頭文件
#include <stdlib.h>

atoi簡介

atoiC語言庫函數名

atoi

atoi原型

int atoi(const char *nptr);

atoiUNICODE

_wtoi()

atoi例子

(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
(2)
//vs2013裏調用printf函數請使用預處理命令#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>

int main()
{
    char a[] = "-100";
    char b[] = "123";
    int c;
    c = atoi(a) + atoi(b);
    printf("c=%d\n", c);
    return 0;
}
執行結果:
c = 23
參考資料