简体   繁体   English

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

[英]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: 如有疑问,请查看手册页:

$ man rand $ man rand

$ man time $ man时间

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.

相关问题 function [-Wimplicit-function-declaration] 的隐式声明 - implicit declaration of function [-Wimplicit-function-declaration] GCC |警告:隐式声明函数&#39;_stricmp&#39;[-Wimplicit-function-declaration] | - GCC |warning: implicit declaration of function '_stricmp' [-Wimplicit-function-declaration]| 函数&#39;scan_s&#39;的隐式声明[-Wimplicit-function-declaration] - Implicit declaration of function 'scan_s' [-Wimplicit-function-declaration] 警告:function 'colcheck' [-Wimplicit-function-declaration] 的隐式声明 - warning: implicit declaration of function ‘colcheck’ [-Wimplicit-function-declaration] 警告:隐式声明 function 'gettimeofday' [-Wimplicit-function-declaration] - warning: implicit declaration of function ‘gettimeofday’ [-Wimplicit-function-declaration] 警告:函数“gets”的隐式声明; 您指的是 &#39;fgets&#39; 吗? [-Wimplicit-function-declaration] - warning: implicit declaration of function ‘gets’; did you mean ‘fgets’? [-Wimplicit-function-declaration] 警告:function 'strcpy' [-Wimplicit-function-declaration] 的隐式声明程序工作,但我如何修复编译器错误 - warning: implicit declaration of function ‘strcpy’ [-Wimplicit-function-declaration] the program work but how i fix the compiler error 我收到此错误“solution.c:9:4: 警告:函数‘stricmp’的隐式声明 [-Wimplicit-function-declaration] x= stricmp(ap,c);” - i get this error "solution.c:9:4: warning: implicit declaration of function 'stricmp' [-Wimplicit-function-declaration] x= stricmp(ap,c);" 错误:function 'rl_replace_line' 的隐式声明在 C99 中无效 [-Werror,-Wimplicit-function-declaration] - error: implicit declaration of function 'rl_replace_line' is invalid in C99 [-Werror,-Wimplicit-function-declaration] 程序警告中的 gettinan 错误:隐式声明 function 'Binsearch' [-Wimplicit-function-declaration] res= Binsearch(arr,n-1,0,x); - gettinan error in the program warning: implicit declaration of function 'Binsearch' [-Wimplicit-function-declaration] res= Binsearch(arr,n-1,0,x);
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM