I am using the Big Nerd Ranch book Objective-C Programming, and it starts out by having us write in C in the first few chapters. In one of my programs it has me create, I use the sleep function. In the book it told me to put #include <stdlib.h>
under the #include <stdio.h>
part. This is supposed to get rid of the warning that says "Implicit declaration of function 'sleep' is invalid in C99". But for some reason after I put #include <stdlib.h>
, the warning does not go away.. This problem does not stop the program from running fine, but I was just curious on which #include
I needed to use!
-
1If you use any mayor IDE(NetBeans,IntelliJ IDEA, Eclipse). type the name of any function, then Alt+Enter it will auto-import the library that has it.– T04435Mar 8, 2016 at 7:39
-
2@T04435: In C libraries are not imported. The compiler does not need them. The linker might link them, but only after the compiler is done. In C the compiler needs a prototype of a function to to use a function. Prototypes typically come in header files (.h).– alkJul 1, 2018 at 11:32
5 Answers
The sleep man page says it is declared in <unistd.h>
.
Synopsis:
#include <unistd.h>
unsigned int sleep(unsigned int seconds);
-
1I had not! Thank you! it was just kind of bothering me, because the book said that the <stdlib.h> would get rid of the warning... weird haha @simonc– trludtFeb 11, 2013 at 18:05
-
1Would it be better to use the sleep() function or time() to create a delay? May 19, 2017 at 17:27
-
@LandonZeKepitelOfGreytBritn: At least the C function
time()
does not created a delay, at least not a well defined delay, based on the arguments passed.– alkSep 10, 2020 at 10:08
sleep
is a non-standard function.
- On UNIX, you shall include
<unistd.h>
. - On MS-Windows,
Sleep
is rather from<windows.h>
.
In every case, check the documentation.
-
4
-
On UNIX, Sleep is actually usleep and it takes microseconds (milliseconds*1000) instead of seconds.– AgostinoFeb 6, 2017 at 14:59
-
7Don't use usleep: "4.3BSD, POSIX.1-2001. POSIX.1-2001 declares this function obsolete; use nanosleep(2) instead. POSIX.1-2008 removes the specification of usleep()." linux.die.net/man/3/usleep Jun 6, 2018 at 8:03
-
1Windows's
Sleep()
and POSIX'sleep()
are not the same. They take different arguments. For former takes milli-seconds, the latter takes seconds!– alkSep 10, 2020 at 10:06
this is what I use for a cross-platform code:
#ifdef _WIN32
#include <Windows.h>
#else
#include <unistd.h>
#endif
int main()
{
pollingDelay = 100
//do stuff
//sleep:
#ifdef _WIN32
Sleep(pollingDelay);
#else
usleep(pollingDelay*1000); /* sleep for 100 milliSeconds */
#endif
//do stuff again
return 0;
}
-
20usleep() was removed in POSIX.1-2008. Use nanosleep(). linux.die.net/man/3/usleep Jun 6, 2018 at 8:06
What is the proper #include for the function 'sleep()'?
sleep()
isn't Standard C, but POSIX so it should be:
#include <unistd.h>
sleep(3)
is in unistd.h
, not stdlib.h
. Type man 3 sleep
on your command line to confirm for your machine, but I presume you're on a Mac since you're learning Objective-C, and on a Mac, you need unistd.h
.