C语言 函数'time'的隐式声明[-Wimplicit-function-declaration]|

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15458393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:44:26  来源:igfitidea点击:

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

csrand

提问by Chathura Dodamgoda

Whenever I try to use srandfunction I get this warning

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

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

and a windows error reportappears when running the compiled file,
I'm a novice to c programming, I found this on a text book, but it doesn't work for me.

Windows错误报告显示,当运行编译的文件
我是一个新手,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?

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

回答by Paul R

You need to make sure that you #includethe right headers, in this case:

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

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

When in doubt, check the man pages:

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

$ man rand

$曼兰特

$ man time

$人时间

One further problem: time()requires an argument, which can be NULL, so your call to srand()should be:

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

srand(time(NULL));

回答by Acsor

Note that time()function uses current time (expressed in seconds since 1970) both in its return value and in its address argument.

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