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

后端 未结 5 971
北海茫月
北海茫月 2020-11-28 23:09

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 us

相关标签:
5条回答
  • 2020-11-28 23:28

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

    Synopsis:

    #include <unistd.h>
    

    unsigned int sleep(unsigned int seconds);

    0 讨论(0)
  • 2020-11-28 23:32

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

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

    #include <unistd.h>
    
    0 讨论(0)
  • 2020-11-28 23:39

    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;
    }
    
    0 讨论(0)
  • 2020-11-28 23:44

    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.

    0 讨论(0)
  • 2020-11-28 23:47

    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.

    0 讨论(0)
提交回复
热议问题