C 庫函數?int atoi(const char *str)?把參數?str?所指向的字符串轉換為一個整數(類型為 int 型)。 測試用例: #include <stdio.h> #include <stdlib.h> #include <string.h>int main() {int val;char str[20];strcpy(str, "98993489");val = atoi(str);printf("字符串值 = %s, 整型值 = %d\n", str, val);strcpy(str, "runoob.com");val = atoi(str);printf("字符串值 = %s, 整型值 = %d\n", str, val);return(0); }
?
輸出結果:
字符串值 = 98993489, 整型值 = 98993489 字符串值 = runoob.com, 整型值 = 0