I am working on a program that receives an input from the user stores it as a string of digits (assume no faulty input) and converts this char string/array back into an integer. I realize I could just store the input as an integer originally but I am trying to practice some conversion methods.
Currently my error involves the function atoi(), which has a "conflicting types" received message.
#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;
}
Here is the error code:
Invoking: GCC C Compiler
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.