温馨提示×

c语言如何将字符串转换为整数

小亿
91
2024-06-03 16:23:13
栏目: 编程语言

在C语言中,可以使用标准库函数atoi()来将字符串转换为整数。atoi()函数的原型如下:

int atoi(const char *str);

示例代码如下所示:

#include <stdio.h>
#include <stdlib.h>

int main() {
    char str[] = "12345";
    int num = atoi(str);
    
    printf("Converted number: %d\n", num);
    
    return 0;
}

上面的示例代码中,将字符串"12345"转换为整数并存储在变量num中,然后将其打印出来。

0