What is #include for the "sleep" function in C?

I use the Big Nerd Ranch Objective-C Programming book, and it starts with what we write in C in the first few chapters. In one of my programs, he creates me, I use the sleep function. I was told in the book that #include <stdlib.h> is under the #include <stdio.h> . This should get rid of the warning that says: "The implicit declaration of the" sleep "function is not valid in C99." But for some reason, after I put #include <stdlib.h> , the warning will not go away. This problem does not prevent the program from working fine, but I was just curious about which #include I needed to use!

+90
c sleep
Feb 11 '13 at 18:02
source share
5 answers

The sleep man page says it is declared in <unistd.h> .

+112
Feb 11 '13 at 18:03
source share

sleep is a non-standard function.

  • On UNIX, you must include <unistd.h> .
  • On MS-Windows, sleep more likely from <windows.h> .

In each case, check the documentation.

+48
Feb 11 '13 at 18:03
source share

this is what i use for 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; } 
+43
Apr 18 '14 at 17:34
source share

For sleep() it should be

 #include <unistd.h> 
+12
Feb 11 '13 at 18:04 on
source share

sleep(3) is in unistd.h , not stdlib.h . Type man 3 sleep at the command line to confirm your machine, but I assume you are on a Mac, since you are learning Objective-C, and on a Mac you need unistd.h .

+4
Feb 11 '13 at 18:04 on
source share



All Articles