4

Both work correctly in my simple test code, but I'd like to know if there is any real difference, or any agreed upon preferences in coding styles.

Sample code enclosed:

#include <stdio.h>
#include <time.h>

int main(int argc, char **argv)
{
    time_t now1, now2;
    time(&now1);
    now2 = time(NULL);

    printf("now1 = %ld\n", now1);
    printf("now2 = %ld\n", now2);

    return 0;
}

EDIT
I just saw Keith Thompson's answer - This question should probably be marked as duplicate.

1
  • 1
    Note: time_t is not defined as a long, so printing with "%ld" is not portable. printf("now1 = %ld\n", (long) now1); is somewhat better. Consider printf("now1 = %s\n", ctime(&now1)); or others. Mar 14, 2017 at 13:56

4 Answers 4

1

There is no difference between the two.

If you do this:

time_t now1, now2;
now2 = time(&now1);

Both now1 and now2 will have the same value.

As a historical note, these would not be the same if run on a Linux 2.4 or earlier kernel 64-bit. There was a bug in the time function where only the lower 32 bits of the passed-in parameter were set (when time_t is 64-bit). So if you used the return value you'd get the right value but if you passed in the address of a time_t you'd get garbage if the variable wasn't initialized.

1

As a side note considering performance: If you use the one with NULL parameter value the compiler will generate different code (most possibly it will xor out the ingoing parameter (usually a register)) than for the case with the parameter (where the code generated will contain an assignment to the ingoing parameter of the actual address), and since regardless of the parameter value the function will return the value of the time, if there is a (valid) address as the parameter the execution might take a few cycles more, since inside the function, there is a check whether to copy the return value to the given address or not, and if there is an address (ie: parameter is not null) a memory copy will be executed.

1

if there is any real difference?

No large difference.

time_t now1, now2;
time(&now1);
now2 = time(NULL);

Not shown is time_t now3 = time(NULL); which follow a popular style of RAII in that the object never exist without a defined value - a short-coming in OP's 2 examples.

or any agreed upon preferences in coding styles?

I prefer this 3rd way, but best to use the style outlined by your group's coding standards. If your group lacks standards, make them as it is more valuable to code in a similar consistent style.

0

There is no difference. In the past, anything larger than int could not be returned as a function return and because of that, the receiving variable had to be passed as a pointer.

6
  • 1
    Do you have a reference for this? I didn't know that, and would like to learn more. Mar 14, 2017 at 13:12
  • 1
    @Storyteller, I checked my 1978 edition of K&R and even that one allowed the return of doubles. So I am wrong in the general case and should refer to the history of the time function, where somewhere this was the case... Mar 14, 2017 at 13:22
  • @StoryTeller, even PDP-11 C had return type double. Mar 14, 2017 at 13:47
  • I vaguely remembered something like that, which is why I asked. But in actuality, the C standard offered an abstraction. Even if something larger than int cannot be "returned", it's up to the implementation to set it up properly (pass the address of the temporary the will hold the result). From the view of the C code, it's still a function returning a value. Mar 14, 2017 at 13:50
  • "anything larger than int could not be returned as a function return" --> I am certain double sqrt(double) is a good counter example. Mar 14, 2017 at 13:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.