在C语言中,可以使用atoi
函数将char
类型转换为int
类型。atoi
函数将输入的字符串参数转换为相应的整数值。
以下是一个示例代码:
#include <stdio.h>
#include <stdlib.h>
int main() {
char ch = '5';
int num = atoi(&ch);
printf("The converted integer is: %d\n", num);
return 0;
}
输出结果为:The converted integer is: 5
。
请注意,atoi
函数只能将一个字符转换为一个整数值。如果要将一个字符串转换为整数值,可以直接传递字符串作为参数,例如:int num = atoi("123");
。
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: c语言栈怎么实现逆序输出