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>.
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):
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;
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