在C语言中,可以使用标准库函数atoi()
或者sscanf()
来将字符串转换为整数。
使用atoi()
函数示例如下:
#include <stdio.h>
#include <stdlib.h>
int main() {
char str[] = "1234";
int num = atoi(str);
printf("The integer is: %d\n", num);
return 0;
}
使用sscanf()
函数示例如下:
#include <stdio.h>
int main() {
char str[] = "1234";
int num;
sscanf(str, "%d", &num);
printf("The integer is: %d\n", num);
return 0;
}
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: c语言中gets函数如何使用