在C语言中,可以使用标准库函数atoi()
来实现将字符串转化为整数。该函数的原型如下:
int atoi(const char *str);
示例如下:
#include <stdio.h>
#include <stdlib.h>
int main() {
char str[] = "12345";
int num = atoi(str);
printf("String: %s\n", str);
printf("Number: %d\n", num);
return 0;
}
运行结果将输出:
String: 12345
Number: 12345
注意:atoi()
函数只能将合法的整数字符串转化为整数,如果字符串不符合整数格式,转化结果会不可预料。
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: C语言switch语句的用法有哪些