代码点读机 / C / atoi

atoi C · 字符串函数

把字符串转换成 int 类型的整数。

怎么用

int n = atoi(字符串);

小例子

#include <stdio.h>
#include <stdlib.h>
int main(void) {
    char* s = "123";
    printf("%d", atoi(s));
    return 0;
}

运行结果

输出 123

提醒:转换失败返回 0,无法区分失败与 0,安全场景请用 strtol

官方文档:https://en.cppreference.com/w/c/string/byte/atoi

在代码里遇到不认识的词?打开代码点读机,点一下就懂 → ← C全部词条