温馨提示×

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

小亿
82
2024-09-26 00:21:57
栏目: 编程语言

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

int atoi(const char *str);

其中,str是指向以空字符结尾的字符串的指针,该字符串表示一个整数。函数返回字符串所表示的整数。

以下是一个简单的示例:

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

int main() {
    char str[] = "12345";
    int num = atoi(str);
    printf("The integer value of the string is: %d\n", num);
    return 0;
}

输出结果:

The integer value of the string is: 12345

0