时间函数语法
为什么 time
函数通常这样使用:
time_t currentTime;
currentTime = time( NULL );
而不是这样:
time_t currentTime;
time( ¤tTime );
第一种方法使用更多只是因为它可以说更具可读性?还是还有其他原因?
谢谢。
编辑:另外,为什么 time
函数要这样设计?为什么有两种设置变量的方法?
Why is the time
function usually used like this:
time_t currentTime;
currentTime = time( NULL );
instead of this:
time_t currentTime;
time( ¤tTime );
Is the first method used more just because it is arguably more readable? Or is there another reason?
Thanks.
Edit: Also, why was the time
function even designed this way? Why have two ways to set the variable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
从函数返回值似乎总是比传递已更改的参数更有意义。
这可能是因为我们学习的语言的函数和子例程不同,不了解今天的这些年轻人。
It always seems to make more sense to return a value from a function than pass a paramter that is changed.
This might be because we learned on languages where functions and subroutines were different, don't know about these youngsters today.
当然,只有 K&R 可能知道真正的答案,但我怀疑这只是一个“事件”,因为历史实施的原因。例如,此函数的设计可能是从
void time(time_t*)
开始的,因为在 ANSI C 之前的某种形式中根本不可能返回time_t
类型的值code> 后来才演变为一个返回值的函数。如果这是解释,那么无论如何保留参数的原因当然是与现有代码的向后兼容性。
Of course only K&R probably know the true answer, but my suspect is that is just an "incident" because of historical implementation reasons. For example may be the design of this function started as
void time(time_t*)
because simply wasn't possible in some form of pre-ansi C to return a value of typetime_t
and only later evolved in a value-returning function.If this is the explanation then the reason for keeping the parameter anyway is of course backward compatibility with existing code.
最常见的格式实际上是
time_t currentTime = time( NULL );
这更短,并且不会使
currentTime
变量处于未初始化状态。该参数是历史性事故,没有任何用处。The most common format is actually
time_t currentTime = time( NULL );
This is shorter, and doesn't leave the
currentTime
variable uninitialized. The parameter is a historic accident and has no use.定时器参数是一个指向 time_t 类型的对象的指针,其中存储了时间值。
或者,该参数可以是空指针,在这种情况下,不使用该参数,但函数仍返回 time_t 对象。
所以你不需要创建一个 time_t 对象。
The timer parameter is a pointer to an object of type time_t, where the time value is stored.
Alternativelly, this parameter can be a null pointer, in which case the parameter is not used, but a time_t object is still returned by the function.
So you don't need to create a time_t object.