C语言如何把字符型转为int型
在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;
}
版权声明
本文仅代表作者观点,不代表米安网络立场。
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。