How can a C function like time() be called without its header file?

Viewed 130

According to documentation I checked, time_t time(time_t *seconds) is declared under <time.h> header file. Yet when I run this code:

#include<stdio.h>
#include<stdlib.h>
int main()
{
    srand(time(NULL));
    printf("%d",rand());
    return 0;
}

without including <time.h>,it works fine, or at least works. How does that happen?

1 Answers

It does not work fine, if you enable the compiler warnings. Without the required header, it'll throw a warning, something like

1347694184/source.c: In function ‘main’:
1347694184/source.c:7:11: warning: implicit declaration of function ‘time’ [-Wimplicit-function-declaration]
     srand(time(NULL));
           ^~~~

Moral of the story: Always use proper compiler settings to warn (and if possible consider warnings as errors). Take care of the warnings, and you'll be on the safer side.

Related