C语言如何把字符型转为int型

   2024-10-15 7440
核心提示:在C语言中,可以使用标准库函数atoi()或者scanf()来将字符型转换为整型。使用atoi()函数:#include stdio.h#include stdlib.hint

在C语言中,可以使用标准库函数atoi()或者scanf()来将字符型转换为整型。

使用atoi()函数:
#include <stdio.h>#include <stdlib.h>int main() {    char c = '5';    int num = atoi(&c); // 将字符型变量转换为整型    printf("The integer value is: %d\n", num);    return 0;}
使用scanf()函数:
#include <stdio.h>int main() {    char c = '5';    int num;    sscanf(&c, "%d", &num); // 将字符型变量转换为整型    printf("The integer value is: %d\n", num);    return 0;}

 
举报打赏
 
更多>同类维修大全
推荐图文
推荐维修大全
点击排行

网站首页  |  关于我们  |  联系方式网站留言    |  赣ICP备2021007278号