[英]implicit declaration of function 'time' [-Wimplicit-function-declaration]|
Whenever I try to use srand
function I get this warning每当我尝试使用srand
function 时,我都会收到此警告
"implicit declaration of function 'time' [-Wimplicit-function-declaration]|"
and a windows error report appears when running the compiled file ,运行编译文件时出现windows错误报告,
I'm a novice to c programming, I found this on a text book, but it doesn't work for me.我是 c 编程的新手,我在教科书中找到了这个,但它对我不起作用。
srand (time());
int x= (rand()%10) +1;
int y= (rand()%10) +1;
printf("\nx=%d,y=%d", x,y);
What do I need to correct this?我需要什么来纠正这个问题?
You need to make sure that you #include
the right headers, in this case: 在这种情况下,您需要确保#include
正确的标头:
#include <stdlib.h> // rand(), srand()
#include <time.h> // time()
When in doubt, check the man pages: 如有疑问,请查看手册页:
One further problem: time()
requires an argument, which can be NULL
, so your call to srand()
should be: 还有一个问题: time()
需要一个参数,它可以是NULL
,所以你对srand()
调用应该是:
srand(time(NULL));
请注意, time()
函数在其返回值和地址参数中使用当前时间(自1970年以秒表示)。
I had this issue, and the problem was that in windows you need to include sys/time.h
, but in linux you need time.h
and I didn't notice.我遇到了这个问题,问题是在 windows 中您需要包含sys/time.h
,但在 linux 中您需要time.h
而我没有注意到。
I fixed this by adding a simple platform check:我通过添加一个简单的平台检查来解决这个问题:
#ifdef _WIN32
#include <sys/time.h>
#else
#include <time.h>
#endif
Note that this is for windows and linux, because that's what I needed for my program.请注意,这是针对 windows 和 linux 的,因为这是我的程序所需要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.