Time function syntax

Why is the time function commonly used as follows:

 time_t currentTime; currentTime = time( NULL ); 

instead of this:

 time_t currentTime; time( &currentTime ); 

Is the first method used more simply because it is perhaps more readable? Or is there another reason?

Thanks.

Edit: Also, why is the time function even designed this way? Why are there two ways to set a variable?

+4
source share
4 answers

It seems that it makes sense to return a value from the function than to pass the changed parameter.

Perhaps this is due to the fact that we studied in languages ​​where functions and routines were different, today I do not know about these young people.

+6
source

Of course, only K & R probably knows the true answer, but my suspect is just an β€œincident” due to historical reasons for implementation. For example, the design of this function may be launched as void time(time_t*) , because it is simply impossible in some form of pre-ansi C to return a value of type time_t and only later develop into a function that returns a value.

If this is an explanation, then the reason for saving the parameter in any case is, of course, backward compatibility with existing code.

+1
source

The most common format is time_t currentTime = time( NULL );

This is shorter and does not leave the currentTime variable uninitialized. This parameter is a historical event and does not make sense.

+1
source

The timer parameter is a pointer to an object of type time_t, where the time value is stored. Alternatively, this parameter may be a null pointer, in which case the parameter is not used, but the time_t function is still returned by the function.

Therefore, you do not need to create a time_t object.

-1
source

Source: https://habr.com/ru/post/1335504/


All Articles