C语言中的时间(NULL)是什么?
我学习了一些基本的 C 函数,并在一些手册中遇到了 time(NULL)
。
这究竟意味着什么?
I learning about some basic C functions and have encountered time(NULL)
in some manuals.
What exactly does this mean?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您可以传入一个指向
time_t
对象的指针,time
将用当前时间填充该对象(返回值与您指向的值相同)。如果您传入NULL
,它只会忽略它并仅返回一个表示当前时间的新time_t
对象。You can pass in a pointer to a
time_t
object thattime
will fill up with the current time (and the return value is the same one that you pointed to). If you pass inNULL
, it just ignores it and merely returns a newtime_t
object that represents the current time.对
time(NULL)
的调用返回当前日历时间(自 1970 年 1 月 1 日以来的秒数)。有关详细信息,请参阅此参考。通常,如果您传入一个指向time_t
变量< /a>,该指针变量将指向当前时间。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 atime_t
variable, that pointer variable will point to the current time.time()
是一个非常非常古老的函数。可以追溯到 C 语言甚至没有long
类型的那一天。曾几何时,获得类似 32 位类型的唯一方法是使用两个int
的数组 - 那是在int
为 16 位的时候。因此,您调用
后,它会将 32 位时间填充到
now[0]
和now[1]
中,一次 16 位。 (这解释了为什么其他与时间相关的函数,例如localtime
和ctime
,也倾向于通过指针接受它们的时间参数。)稍后,dmr 完成了向编译器添加
long
,所以你可以开始说稍后,有人意识到如果
time()
继续并返回值,而不是仅仅通过指针填充它。但是 - 向后兼容性是一件很棒的事情 - 为了所有仍在执行time(&now)
的代码的利益,time()
函数必须继续支持指针参数。这就是为什么 - 这就是为什么向后兼容性并不总是那么美妙的事情,毕竟 - 如果您使用返回值,您仍然必须将 NULL 作为指针传递:(当然,后来我们开始使用
time_t
而不是简单的long
表示时间,因此,例如,它可以更改为 64 位类型,从而避免 y2.038k 问题。)[PS 我实际上不确定
int [2 ]
到long
,以及添加返回值的更改发生在不同的时间;它们可能同时发生。但请注意,当时间表示为数组时,它必须通过指针填充,它不能作为值返回,因为 C 函数当然不能返回数组。 ]time()
is a very, very old function. It goes back to a day when the C language didn't even have typelong
. Once upon a time, the only way to get something like a 32-bit type was to use an array of twoint
s — and that was whenint
s were 16 bits.So you called
and it filled the 32-bit time into
now[0]
andnow[1]
, 16 bits at a time. (This explains why the other time-related functions, such aslocaltime
andctime
, tend to accept their time arguments via pointers, too.)Later on, dmr finished adding
long
to the compiler, so you could start sayingLater 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 doingtime(&now)
, thetime()
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:(Later still, of course, we started using
time_t
instead of plainlong
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]
tolong
, 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.]time
函数返回自某个时间点(在 Unix 系统上,自 UTC 1970 年 1 月 1 日午夜以来)以来的当前时间(作为time_t
值),以秒为单位,并且需要一参数,一个存储时间的time_t
指针。传递NULL
作为参数会导致time
将时间作为正常返回值返回,但不会将其存储在其他地方。The
time
function returns the current time (as atime_t
value) in seconds since some point (on Unix systems, since midnight UTC January 1, 1970), and it takes one argument, atime_t
pointer in which the time is stored. PassingNULL
as the argument causestime
to return the time as a normal return value but not store it anywhere else.Time
:返回自 1970 年 1 月 1 日以来经过的时间(以秒为单位)Time
: It returns the time elapsed in seconds since the epoch 1 Jan 1970您必须参考ctime 文档。
time
是一个函数,它采用一个time_t *
类型的参数(指向time_t
对象的指针)并为其分配当前时间。除了传递此指针之外,您还可以传递 NULL,然后使用返回的 time_t 值。You have to refer to the documentation for ctime.
time
is a function that takes one parameter of typetime_t *
(a pointer to atime_t
object) and assigns to it the current time. Instead of passing this pointer, you can also passNULL
and then use the returned time_t value instead.您可以传入一个指向
time_t
对象的指针,该时间将用当前时间填充(并且返回值与您指向的值相同)。如果您传入NULL
,它只会忽略它并仅返回一个表示当前时间的新time_t
对象。Nb:time(&timer);
相当于timer = time(NULL);
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 inNULL
, it just ignores it and merely returns a newtime_t
object that represents the current time.Nb:time(&timer);
is equivalent totimer = time(NULL);
这将打印 1481986944.00(此时)。
this prints 1481986944.00 (at this moment).