我正在开发一个程序,该程序接收用户的输入并将其存储为数字字符串(假设没有错误输入)并将此 char 字符串/数组转换回整数。我意识到我最初可以将输入存储为整数,但我正在尝试练习一些转换方法。
目前我的错误涉及函数 atoi(),它收到了“类型冲突”的消息。
#include <stdio.h>
#include <stdlib.h>
int atoi(char s[])
{
int i, n;
n = 0;
for (i = 0; s[i] >= '0' && s[i] <= '9'; ++i)
{
n = 10 * n + (s[i] - '0');
}
return n;
}
int main(void)
{
char str[5];
int c, i;
printf("Please enter a number");
for (i = 0; (c = getchar()) != '\n'; ++i)
{
str[i] = c;
}
printf("%d", atoi(str));
return EXIT_SUCCESS;
}
错误代码如下:
调用:GCC C 编译器
gcc -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/Chapter2.d" -MT"src/Chapter2.d" -o "src/Chapter2.o" "../src/Chapter2.c"
../src/Chapter2.c:50:5: error: conflicting types for ‘atoi’
int atoi(char s[])
最佳答案
atoi()
标准库里有吗#include<stdlib.h>
将您的函数名称更改为其他名称
关于C 编程 : store getchar as a char array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27784907/