池州网站建设,广西网站建设-好发信息网,餐饮网站界面,百度pc网页版文章目录 1 atoi()函数1.1 函数原型1.2 参数1.3 返回值1.4 转换机制1.5 示例1.5.1 示例1 1 atoi()函数
1.1 函数原型
atoi()#xff1a;将str指向的字符串转换为整数#xff0c;函数原型如下#xff1a;
int atoi(const char *str);1.2 参数
atoi()函数只有一个参数str将str指向的字符串转换为整数函数原型如下
int atoi(const char *str);1.2 参数
atoi()函数只有一个参数str
参数str是指向要转换为整数的字符串的指针类型为char*型。
1.3 返回值
atoi()函数的返回值类型为int型。
转换成功返回转换后的int型整数转换失败返回0值。
1.4 转换机制
atoi()函数将str指向的字符串转换为int型整数atoi()函数将字符串转换为整数的过程类似于scanf()函数从stdin中读取字符赋值给变量的过程 1有效字符为±符号和数字字符1-9 2跳过所有空字符 3如果第一非空字符是无效字符则转换失败返回0值 4如果第一非空字符是有效字符则转换继续直至遇到第一个无效字符为止无效字符包括字母、标点符号和空字符等。
C语言标准描述如下
1. Interprets an integer value in a byte string pointed to by str.
2. The implied radix is always 10.
3. Discards any whitespace characters until the first non-whitespace character is found, then takes as many characters as possible to form a valid integer number representation and converts them to an integer value.
4. The valid integer value consists of the following parts:--- (optional) plus or minus sign--- numeric digits
5. If the value of the result cannot be represented, i.e. the converted value falls out of range of the corresponding return type, the behavior is undefined.1.5 示例
1.5.1 示例1
代码如下在这里插入代码片所示
int main()
{printf(%d\n, atoi(123));printf(%d\n, atoi(123a));printf(%d\n, atoi(a123));printf(%d\n, atoi( 123));printf(%d\n, atoi(1 23));printf(%d\n, atoi(123));printf(%d\n, atoi(-123));printf(%d\n, atoi(1.23));printf(%d\n, atoi(2147483648));return 0;
}代码运行结果如下图所示