簡體   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