我刚刚开始使用 C,正在尝试学习 ATOL 函数。有人能告诉我为什么它一直打印 0 吗?我知道这意味着无法执行转换,但我不确定为什么。
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
int i = atoi (" bl149");
printf("%d\n", i);
return 0;
}
最佳答案
atoi 基本上将具有数字的字符串转换为整数 1,无论它将转换什么,它将成为它的返回值。 或者 更准确地说,atoi 函数从字符串的开头开始检查。如果它有数字(仅从头开始),那么它将以整数形式返回该值。 下面的例子将清除这个概念 例如
atoi("1234")
--> it will convert string "1234" in to integer and return it
--> i.e. ouput is 1234
atoi("1234abcd") --> i.e. ouput is 1234
atoi("a1234abcd") --> i.e. ouput is 0
在你的情况下,因为你的字符串从 b ("b1149") 开始,所以它将返回 0
关于c - 为什么 ATOI 在此代码中返回 0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13758373/