C语言 atoi 函数详解:字符串转整数
atoi 函数是 C 标准库中的一个函数,用于将字符串转换为整数。它的函数原型如下:
int atoi(const char *str);
参数 'str' 是需要转换的字符串,返回值是转换后的整数。
例如:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char num[] = '12345';
int result = atoi(num);
printf('The result is: %d\n', result);
return 0;
}
输出结果为:
The result is: 12345
如果字符串中包含非数字字符,那么只会转换前面的数字部分,忽略后面的非数字字符。如果字符串不能转换为整数,则返回 0。
原文地址: https://www.cveoy.top/t/topic/nHZX 著作权归作者所有。请勿转载和采集!