What is time(NULL) in C?

CTime

C Problem Overview


I learning about some basic C functions and have encountered time(NULL) in some manuals.

What exactly does this mean?

C Solutions


Solution 1 - C

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.

Solution 2 - C

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.

Solution 3 - C

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 ints — and that was when ints 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, after all — 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.]

Solution 4 - C

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.

Solution 5 - C

Time : It returns the time elapsed in seconds since the epoch 1 Jan 1970

Solution 6 - C

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.

Solution 7 - C

int main (void)
{   
    //print time in seconds from 1 Jan 1970 using c   
    float n = time(NULL);   
    printf("%.2f\n" , n);      
}      

this prints 1481986944.00 (at this moment).

Solution 8 - C

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.

Nb:time(&timer); is equivalent to timer = time(NULL);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondmubuView Question on Stackoverflow
Solution 1 - CjasonView Answer on Stackoverflow
Solution 2 - CjonscaView Answer on Stackoverflow
Solution 3 - CSteve SummitView Answer on Stackoverflow
Solution 4 - CjwodderView Answer on Stackoverflow
Solution 5 - CMobView Answer on Stackoverflow
Solution 6 - CMahmoud Al-QudsiView Answer on Stackoverflow
Solution 7 - Ck_clark.90View Answer on Stackoverflow
Solution 8 - CMd. Al Amin BhuiyanView Answer on Stackoverflow