implicit declaration of function 'time' [-Wimplicit-function-declaration]|
Asked Answered
R

3

28

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?

Rational answered 17/3, 2013 at 7:6 Comment(0)
T
48

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));
Tuckerbag answered 17/3, 2013 at 7:9 Comment(3)
thanks for answering.. i hv included the stdlib.h ,but after including time.h it gives me another error error: too few arguments to function 'time'Rational
the code i included works fine on online compilers like code pad.. im using codeblocks ide on my pcRational
man rand may not work as expected. There are other utilities named randPrimate
A
1

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

Antemortem answered 17/3, 2013 at 8:51 Comment(0)
A
0

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.

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.

Apish answered 29/8, 2022 at 15:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.