在C语言中,可以使用标准库函数atoi()
或strtol()
来将字符串转换成数字。
- 使用
atoi()
函数:
#include <stdio.h>
#include <stdlib.h>
int main() {
char str[] = "12345";
int num = atoi(str);
printf("The converted number is: %d\n", num);
return 0;
}
- 使用
strtol()
函数:
#include <stdio.h>
#include <stdlib.h>
int main() {
char str[] = "12345";
long num = strtol(str, NULL, 10);
printf("The converted number is: %ld\n", num);
return 0;
}
其中,strtol()
函数可以将字符串转换成长整型数字,第一个参数为要转换的字符串,第二个参数为一个指针,用于存放转换后的字符串的结尾位置,第三个参数为转换的基数,一般为10。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,请发送邮件至 55@qq.com 举报,一经查实,本站将立刻删除。转转请注明出处:https://www.szhjjp.com/n/981656.html