0

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[])
3
  • 1
    I don't see you terminating the string anywhere. The random memory after the string might just contain legal digits. Also, if user enters more than 5 digits you may segfault. Commented Jan 5, 2015 at 17:47
  • Conflicting types error indicates you have multiple definitions for atoi. Commented Jan 5, 2015 at 17:49
  • 1
    the atoi() is a function in prototyped in one of the header files you included. Your implementation has a different prototype. This is a very good reason to call your functions by different names that those 'system' functions. For instance, if you called it 'myAtoi()' the problem would go away. Commented Jan 5, 2015 at 21:07

1 Answer 1

3

atoi() is there in the standard library #include<stdlib.h> change your function name to something else

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.