Implicit declaration of funtion 'abs' is invalid in C99


#1

printf("The absolute value of -5 is %d\n", abs(-5));

any ideas what this warning message means and/or how to get rid of it?


#2

You need to include the stdlib.h header file.

Mitch


#3

Hi all.

I started working through the book last night and this is the first problem I’ve come across… I’ve included the header file but still get the error. It’s not stopping me from continuing but it doesn’t feel right ignoring it.

#include <stdio.h>
#include <stdlib.h>
void congratulateStudent(char *student, char *course, int numDays)
{
printf("%s has done as much %s Prgramming as I could fit into %d days.\n", student, course, numDays);

}

int main(int argc, const char * argv[])
{

congratulateStudent("Mark", "Cocoa", 5);
sleep(2);
congratulateStudent("Bo", "Objective-C", 2);
sleep(2);
 congratulateStudent("Mike", "Python", 5);
sleep(2);
congratulateStudent("Ted", "iOS", 5);

return 0;

}

Any ideas?

Cheers

Phil


#4

Phil, I think your issue is slightly different. I don’t see abs used anywhere in your code, but I do see sleep. To use sleep, I believe you need:


#5

Thank you for taking the time to reply.

The book asks you to use #include <stdlib.h> but what you’ve suggested has rectified the problem… strange.

Thanks again

Phil


#6

I’m not sure if that is an error, or if sleep moved, or what, but unistd.h is definitely the current home for that function. Good luck with the rest of the book, I just recently completed it and it was excellent brain exercise!


#7

I’m really enjoying so far - I’ve spent many frustrated hours trying to get my head around other tutorials and this seems to be a breath of fresh air. So far I’ve understood it all and feel quite confident with it… I wonder how long that’ll last!?

Are you new to Xcode and objective C?


#8

Sort of. I actually had the privilege to attend the iOS BootCamp at BNR last year, but then I had a long spell of other assignments that didn’t leave me any time for programming. You’ve heard the phrase “use it or lose it”? I felt that I had forgotten a lot of stuff, and I wanted to fill in some conceptual holes anyway, so I bought the whole current set of BNR books, started with the Objective-C one, and am working my way through (I’m in the middle of the Cocoa one right now). It’s nice to be able to go slow and mess around with the code. Having Aaron and the other instructors available online is a big benefit too, and one that most people probably don’t think about when they are looking for this kind of self-paced material.

I’d recommend the instructor-led BNR courses without reservation, but clear your brainspace and be prepared to drink from the firehose.