Getting warning in C for 'atoi' function

user3249 picture user3249 · Dec 26, 2013 · Viewed 57.1k times · Source

I'm currently coding for a challenge question in a book I'm reading. My code executes perfectly with the correct output, but i"m getting a warning in my code and I'm just wondering why.

I'm getting a warning on the line that reads:

int countdownStart = atoi(numInput);

The warning I'm getting says:

Implicit declaration of function 'atoi' is invalid in C99

#import <readline/readline.h>
#import <stdio.h>

int main(int argc, const char * argv[]){
    printf("Who is cool? ");
    const char *name = readline(NULL);
    printf("%s is cool!\n\n", name);

    printf("What should I start counting? ");
    const char *numInput = readline(NULL);
    int countdownStart = atoi(numInput);
    for (int i = countdownStart; i >= 0; i--){
        if (i % 3 == 0){
            printf("%d\n", i);
            if (i % 5 == 0){
                printf("Found one!\n");
            }
        }
    }

    return 0;
}

Answer

Cedric Morent picture Cedric Morent · Dec 26, 2013

You have to include stdlib.h

#include <stdlib.h>

Next time you encounter similar warnings just run man atoi and the manual pages should state that which header file should be included.