繁体   English   中英

function '时间' [-Wimplicit-function-declaration]| 的隐式声明

[英]implicit declaration of function 'time' [-Wimplicit-function-declaration]|

每当我尝试使用srand function 时,我都会收到此警告

"implicit declaration of function 'time' [-Wimplicit-function-declaration]|" 

运行编译文件时出现windows错误报告
我是 c 编程的新手,我在教科书中找到了这个,但它对我不起作用。

  srand (time());  
  int x= (rand()%10) +1;  
  int y= (rand()%10) +1;  
  printf("\nx=%d,y=%d", x,y); 

我需要什么来纠正这个问题?

在这种情况下,您需要确保#include正确的标头:

#include <stdlib.h>  // rand(), srand()
#include <time.h>    // time()

如有疑问,请查看手册页:

$ man rand

$ man时间

还有一个问题: time()需要一个参数,它可以是NULL ,所以你对srand()调用应该是:

srand(time(NULL));

请注意, time()函数在其返回值和地址参数中使用当前时间(自1970年以秒表示)。

我遇到了这个问题,问题是在 windows 中您需要包含sys/time.h ,但在 linux 中您需要time.h而我没有注意到。

我通过添加一个简单的平台检查来解决这个问题:

#ifdef _WIN32
#include <sys/time.h>
#else
#include <time.h>
#endif

请注意,这是针对 windows 和 linux 的,因为这是我的程序所需要的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM