Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

srand

Whenever I try to use srand function I get this warning

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

and a windows error report appears 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.

  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?

like image 870
Chathura Dodamgoda Avatar asked Mar 17 '13 07:03

Chathura Dodamgoda


2 Answers

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

#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:

srand(time(NULL));
like image 181
Paul R Avatar answered Oct 24 '22 05:10

Paul R


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

like image 1
Acsor Avatar answered Oct 24 '22 04:10

Acsor