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!
Asked
Active
Viewed 4.4e+01k times
147
-
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. – T04435 Mar 08 '16 at 07: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). – alk Jul 01 '18 at 11:32
5 Answers
195
The sleep man page says it is declared in <unistd.h>.
Synopsis:
#include <unistd.h>
unsigned int sleep(unsigned int seconds);
Daniel Selvan
- 749
- 8
- 22
simonc
- 40,917
- 12
- 82
- 102
-
1I had not! Thank you! it was just kind of bothering me, because the book said that the
would get rid of the warning... weird haha @simonc – trludt Feb 11 '13 at 18:05 -
1Would it be better to use the sleep() function or time() to create a delay? – LandonZeKepitelOfGreytBritn May 19 '17 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. – alk Sep 10 '20 at 10:08
80
sleep is a non-standard function.
- On UNIX, you shall include
<unistd.h>. - On MS-Windows,
Sleepis rather from<windows.h>.
In every case, check the documentation.
md5
- 22,897
- 3
- 42
- 92
-
4
-
On UNIX, Sleep is actually usleep and it takes microseconds (milliseconds*1000) instead of seconds. – Agostino Feb 06 '17 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()."_ https://linux.die.net/man/3/usleep – Jetski S-type Jun 06 '18 at 08:03
-
1Windows's `Sleep()` and POSIX' `sleep()` are **not** the same. They take different arguments. For former takes milli-seconds, the latter takes seconds! – alk Sep 10 '20 at 10:06
72
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;
}
Romain VIOLLETTE
- 906
- 8
- 9
-
20usleep() was removed in POSIX.1-2008. Use nanosleep(). https://linux.die.net/man/3/usleep – Jetski S-type Jun 06 '18 at 08:06
15
What is the proper #include for the function 'sleep()'?
sleep() isn't Standard C, but POSIX so it should be:
#include <unistd.h>
alk
- 68,300
- 10
- 92
- 234
9
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.
Carl Norum
- 210,715
- 34
- 410
- 462