atoi函式--把引數 str 所指向的字串轉換為一個整數(型別為 int 型)
阿新 • • 發佈:2020-08-01
C 庫函式 -atoi()
描述
C 庫函式int atoi(const char *str)把引數str所指向的字串轉換為一個整數(型別為 int 型)。
宣告
下面是 atoi() 函式的宣告。
int atoi(const char *str)
引數
- str-- 要轉換為整數的字串。
返回值
該函式返回轉換後的長整數,如果沒有執行有效的轉換,則返回零。
例項
下面的例項演示了 atoi() 函式的用法。
#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