这两种方法都在我的简单测试代码中正常工作,但我想知道是否存在任何真正的区别或任何编程风格上的约定偏好。
附带示例代码:
#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;
}
编辑
我刚刚看到Keith Thompson的回答 - 这个问题可能应该标记为重复。
time_t
未定义为long
,因此使用"%ld"
打印不具有可移植性。printf("now1 = %ld\n", (long) now1);
稍微好一些。考虑使用printf("now1 = %s\n", ctime(&now1));
或其他方法。 - chux - Reinstate Monica