7

When I use the time() function (i.e., just randomize seed for rand() ) but not include the header file time.h, it works for C. For example:

#include <stdio.h>
#include <stdlib.h>

int main()
{
  int i;
  srand(time(NULL));

  for(i=0;i<10;i++){
    printf("\t%d",rand()%10);
  }
  printf("\n");
  return 0;
}

When I try to compile the code above, g++ cannot compile it since time.h isn't included. But gcc can.

$gcc ra.c 
$./a.out 
    4       5       2       4       8       7       3       8       9       3
$g++ ra.c 
ra.c: In function ‘int main()’:
ra.c:8:20: error: ‘time’ was not declared in this scope
 srand(time(NULL));
                ^

Is it related with version of gcc or just a difference between C/C++ ?

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
maynak
  • 173
  • 6
  • 2
    _`#include `_ IIRC `stdlib.h` doesn't guarantee to include `time.h`. – πάντα ῥεῖ May 16 '15 at 13:02
  • 2
    Take a look at `` ``; the C++ and C versions differ because conditional macros are used there. Maybe it has something to do with that. BTW, use `` and `` (or better the appropriate C++ headers) in C++ (if you didn't know). – cadaniluk May 16 '15 at 13:06

2 Answers2

8

You should include <time.h> for time(2) and turn on the warnings. In C, a function with no visible prototype is assumed to return int (which has been deprecated since C99). So compiling with gcc seems fine while g++ doesn't.

Compile with:

gcc -Wall -Wextra -std=c99 -pedantic-errors file.c

and you'll see gcc also complains about it.

P.P
  • 117,907
  • 20
  • 175
  • 238
  • 1
    The "implicit `int`" rule wasn't deprecated in C99, it was removed completely. Calling a function with no visible prototype is a constraint violation in C99 and later, requiring a diagnostic. (GCC does not conform to C99 by default.) – Keith Thompson May 16 '15 at 19:12
  • It was deprecated in the original ansi C89 spec (a holdover from pre-ansi K&R C), and removed completely in C99. – Chris Dodd May 17 '23 at 00:45
2

C89/C90 (commonly, but incorrectly, referred to as "ANSI C") had an "implicit int" rule. If you called a function with no visible declaration, the compiler would effectively create an implicit declaration assuming that the function takes arguments of the types that appear in the call and returns int.

The time function takes an argument of type time_t* and returns a value of type time_t. So given a call

time(NULL)

with no visible declaration, the compiler will generate code as if it took an argument of the type of NULL (which might be int) and returns an int result. Given

srand(time(NULL))

the value returned by time(NULL) will then be implicitly converted from int to the `unsig

If int, time_t, and time_t* all happen to be, say, 32 bits the call is likely to work. If they're of different sizes, it's likely to behave badly. (If NULL is of type void*, it's likely that void* and time_t*, though they're incompatible, are likely to have the same representation -- but the return type is still likely to be a problem.)

The solution, of course, is don't do that. If you use a standard library function, always provide a #include directive for the header that declares it.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631