什么是用C函数“睡眠”正确的#include?(What is the proper #includ

2019-07-19 17:18发布

我现在用的大牧场书呆子书Objective-C编程,它具有我们C语言编写的前几章开始了。 在我的计划已经创造了我一个,我用的是睡眠功能。 在这本书中它告诉我把#include <stdlib.h>#include <stdio.h>的一部分。 这应该摆脱,上面写着“功能‘休眠’的隐式声明是无效C99”的警告。 但由于某些原因后,我把#include <stdlib.h> ,警告将不会消失。这问题不从运行正常停止程序,但我只是好奇,在其上#include我需要使用!

Answer 1:

睡眠手册页说,它在声明<unistd.h>



Answer 2:

sleep是一个非标准功能。

  • 在UNIX中,应包括<unistd.h>
  • 在MS-Windows, Sleep是相当从<windows.h>

在任何情况下,查看文档。



Answer 3:

这是我用一个跨平台的代码:

#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;
}


Answer 4:

对于sleep()它应该是

#include <unistd.h>


Answer 5:

sleep(3)是在unistd.h ,不stdlib.h 。 类型man 3 sleep在命令行上,以确认你的机器,但我相信你是一个Mac上,因为你学习Objective-C,并在Mac上,你需要unistd.h



文章来源: What is the proper #include for the function 'sleep' in C?
标签: c sleep