成功時,函式返回轉換後的整數,型別為 int。 如果轉換後的值超出了 int 可表示的值的範圍,將導致未定義行為。如果可能出現這種情況,請參見 strtol 以獲取更健壯的跨平臺替代方案。
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* atoi example */
#include <stdio.h> /* printf, fgets */
#include <stdlib.h> /* atoi */
int main ()
{
int i;
char buffer[256];
printf ("Enter a number: ");
fgets (buffer, 256, stdin);
i = atoi (buffer);
printf ("The value entered is %d. Its double is %d.\n",i,i*2);
return 0;
}
輸出
Enter a number: 73
The value entered is 73. Its double is 146.
資料競爭
訪問由 str 指向的陣列。
異常 (C++)
無異常保證:此函式從不丟擲異常。
如果 str 沒有指向一個有效的 C 字串,或者轉換後的值超出了 int 可表示的值的範圍,將導致未定義行為。