155

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!

alk
  • 69,737
  • 10
  • 105
  • 255
trludt
  • 1,801
  • 2
  • 13
  • 15
  • 1
    If 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

6 Answers6

208

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

Synopsis:

#include <unistd.h>

unsigned int sleep(unsigned int seconds);

Daniel Selvan
  • 959
  • 10
  • 23
simonc
  • 41,632
  • 12
  • 85
  • 103
  • 1
    I 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
  • 1
    Would 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
82

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.

md5
  • 23,373
  • 3
  • 44
  • 93
  • 4
    w.r.t. the C standard. w.r.t. POSIX, it is – ivotron Feb 10 '14 at 18:48
  • On UNIX, Sleep is actually usleep and it takes microseconds (milliseconds*1000) instead of seconds. – Agostino Feb 06 '17 at 14:59
  • 8
    Don'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
  • 1
    Windows'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;
}
Lila Viollette
  • 926
  • 9
  • 9
16

What is the proper #include for the function 'sleep()'?

sleep() isn't Standard C, but POSIX so it should be:

#include <unistd.h>
alk
  • 69,737
  • 10
  • 105
  • 255
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
  • 219,201
  • 40
  • 422
  • 469
1

Given that sleep is a non-standard function, I created a sleep function with the standard library time.h

#include <time.h>

void sleep(double s) {
    time_t cur_time = time(NULL);
    while ((difftime(time(NULL), cur_time)) < s);
}
Blackjack
  • 1,322
  • 1
  • 16
  • 21
  • Taking the CPU into a While-Loop is NOT a sleep!. a sleep function is to get the CPU to actually sleep and conserve power, your function does the exact opposite, I hope this makes sense, – Heider Sati Nov 23 '22 at 14:47