I learning about some basic C functions and have encountered time(NULL)
in some manuals.
What exactly does this mean?
You have to refer to the documentation for ctime. time
is a function that takes one parameter of type time_t *
(a pointer to a time_t
object) and assigns to it the current time. Instead of passing this pointer, you can also pass NULL
and then use the returned time_t value instead.
The call to time(NULL)
returns the current calendar time (seconds since Jan 1, 1970). See this reference for details. Ordinarily, if you pass in a pointer to a time_t
variable, that pointer variable will point to the current time.
Time
: It returns the time elapsed in seconds since the epoch 1 Jan 1970
You can pass in a pointer to a time_t
object that time
will fill up with the current time (and the return value is the same one that you pointed to). If you pass in NULL
, it just ignores it and merely returns a new time_t
object that represents the current time.
[Answer copied from a duplicate, now-deleted question.]
time()
is a very, very old function. It goes back to a day when the C language didn't even have type long
. Once upon a time, the only way to get something like a 32-bit type was to use an array of two int
s -- and that was when int
s were 16 bits.
So you called
int now[2];
time(now);
and it filled the 32-bit time into now[0]
and now[1]
, 16 bits at a time. (This explains why the other time-related functions, such as localtime
and ctime
, tend to accept their time arguments via pointers, too.)
Later on, dmr finished adding long
to the compiler, so you could start saying
long now;
time(&now);
Later still, someone realized it'd be useful if time()
went ahead and returned the value, rather than just filling it in via a pointer. But -- backwards compatibility is a wonderful thing -- for the benefit of all the code that was still doing time(&now)
, the time()
function had to keep supporting the pointer argument. Which is why -- and this is why backwards compatibility is not always such a wonderful thing -- if you're using the return value, you still have to pass NULL as a pointer:
long now = time(NULL);
(Later still, of course, we started using time_t
instead of plain long
for times, so that, for example, it can be changed to a 64-bit type, dodging the y2.038k problem.)
[P.S. I'm not actually sure the change from int [2]
to long
, and the change to add the return value, happened at different times; they might have happened at the same time. But note that when the time was represented as an array, it had to be filled in via a pointer, it couldn't be returned as a value, because of course C functions can't return arrays.]
The time
function returns the current time (as a time_t
value) in seconds since some point (on Unix systems, since midnight UTC January 1, 1970), and it takes one argument, a time_t
pointer in which the time is stored. Passing NULL
as the argument causes time
to return the time as a normal return value but not store it anywhere else.