Atoi() error 99

I’m working on the last challenge in this chapter, and some problem.
I can’t use atoi() function. The compiler says “Implicit declaration of function ’ atoi’ is invalid in C99”.
It should be working because I already put #<readline/readline.h>.

Any advice?

Thanks,

You need to also include stdlib.h at the beginning, then it works. Unfortunately, the book is unclear about this, and the code samples are not complete.

It is interesting still. Another exercise in what happens when code uses the “atoi” function which reads a char string as an integer. Wonder what is really going on in “atoi”. Lots of reference material on “atoi” on the web. I used it like this (added some other variable for fun):

#include <stdio.h>
//#include <stdlib.h>
#include <readline/readline.h>

int main(int argc, const char * argv[])
{
printf(“Input a number for starting.\n”);
const char *readNum = readline(NULL);
int *num = atoi(readNum);

int i= num;
int j=7;
while (i>= 0) {
    printf("Trying %d.\n",i);
    
    if (i%j == 0) {
        printf("\n %d is evenly divided by %d!\n\n", i, j);
        }
    i= i-3;
}
return 0;

Thanks BigNerdRanch,

nubitoobc :bulb:

[code]#include <stdio.h>
#include <readline/readline.h>

int main(int argc, const char * argv[])
{
printf(“where to start?\n”);
const char *num = readline(NULL);
printf(“Starting from %s\n\n”, num);
int x = atoi(num);
for (x ; x >= 0; x-=3) {
printf("%d\n", x);
if (x % 5 == 0) {
printf(“found one!\n”);
}
}
printf(“done!\n”);
}
[/code]
This code works fine but it shows a warning without adding the stdlib.h

Challenge: Write your own atoi (without cheating.)

[quote]int main(int argc, const char * argv[]) { printf("Input a number for starting.\n"); const char *readNum = readline(NULL); int *num = atoi (readNum); ...[/quote]
Why assign to int*?

Hello,

Thanks for the explanation of the error 99. Now it’s ok, BUT…

Here is my output:

Where should I start? 4422
^C
99
96
93
90
Found one!
87
84
81
78
75
Found one!
72
69
66
63
60
Found one!
57
54
51
48
45
Found one!
42
39
36
33
30
Found one!
27
24
21
18
15
Found one!
12
9
6
3
0
Found one!

I really wonder why it doesn’t start with 42 and why it starts with ^C.

My code is:

#include <readline/readline.h>
#include <stdio.h>
#include <stdlib.h>


int main(int argc, const char * argv[]) {
    printf("Where should I start? ");
    const char *userInput = readline(NULL);
    int i = atoi (userInput);
    for (i = 99; i>-1; i=i-3){
        printf("%d\n", i);
        if (i%5==0)
            printf("Found one!\n");
    }
    return 0;
    
}

And XCode debug seems to be totally ok: “Build succeeded, No issues”

Does someone knows what is going on?

Thank you!!!

[quote]I really wonder why it doesn’t start with 42 and why it starts with ^C.

My code is:

#include <readline/readline.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, const char * argv[]) {
    printf("Where should I start? ");
    const char *userInput = readline(NULL);
    int i = atoi (userInput);
    for (i = 99; i>-1; i=i-3){
        printf("%d\n", i);
        if (i%5==0)
            printf("Found one!\n");
    }
    return 0;
}

[/quote]
You are starting the for loop with i = 99, not with the value read from the console.

Write that for loop like this:

 ...
int i = atoi (userInput);
for (; i>-1; i=i-3){
     ...
}
...