在C语言中可以使用atoi
函数将字符串转换为整数。atoi
函数的原型如下:
int atoi(const char *str);
atoi
函数将参数str
指向的字符串转换为一个整数并返回。具体的实现如下:
#include <stdio.h>
#include <stdlib.h>
int main() {
char str[] = "12345";
int num = atoi(str);
printf("The converted integer is %d\n", num);
return 0;
}
输出结果:
The converted integer is 12345
注意,atoi
函数会忽略字符串中的空格字符,并且当字符串无法转换为整数时,atoi
函数会返回0。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
相关推荐:c语言整数转换为字符串怎么实现