C语言 C中的时间(NULL)是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7550269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 09:42:49  来源:igfitidea点击:

What is time(NULL) in C?

ctime

提问by dmubu

I learning about some basic C functions and have encountered time(NULL)in some manuals.

我学习了一些基本的 C 函数,并time(NULL)在一些手册中遇到过。

What exactly does this mean?

这到底是什么意思?

采纳答案by jason

You can pass in a pointer to a time_tobject that timewill fill up with the current time (and the return value is the same one that you pointed to). If you pass in NULL, it just ignores it and merely returns a new time_tobject that represents the current time.

您可以传入一个指向将填充当前时间的time_t对象的指针time(返回值与您指向的值相同)。如果你传入NULL,它只是忽略它,只返回一个time_t代表当前时间的新对象。

回答by jonsca

The call to time(NULL)returns the current calendar time (seconds since Jan 1, 1970). See this referencefor details. Ordinarily, if you pass in a pointer to a time_tvariable, that pointer variable will point to the current time.

调用time(NULL)返回当前日历时间(自 1970 年 1 月 1 日以来的秒数)。有关详细信息,请参阅此参考资料。通常,如果您传入一个指向time_t变量的指针,该指针变量将指向当前时间。

回答by Steve Summit

[Answer copied from a duplicate, now-deleted question.]

[从重复的、现已删除的问题中复制的答案。]

time()is a very, very old function. It goes back to a day when the C language didn't even have type long. Once upon a time, the only way to get something like a 32-bit type was to use an array of two ints -- and that was when ints were 16 bits.

time()是一个非常非常古老的功能。它可以追溯到 C 语言甚至没有 type 的那一天long。曾几何时,获得类似 32 位类型的唯一方法是使用两个ints的数组——那时ints 是 16 位。

So you called

所以你打电话

int now[2];
time(now);

and it filled the 32-bit time into now[0]and now[1], 16 bits at a time. (This explains why the other time-related functions, such as localtimeand ctime, tend to accept their time arguments via pointers, too.)

并将 32 位时间填充到now[0]和 中now[1],一次填充16 位。(这解释了为什么其他与时间相关的函数,例如localtimeand ctime,也倾向于通过指针接受它们的时间参数。)

Later on, dmrfinished adding longto the compiler, so you could start saying

稍后,dmr完成添加long到编译器,因此您可以开始说

long now;
time(&now);

Later 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 doing time(&now), the time()function had to keep supporting the pointer argument. Which is why -- and this is why backwards compatibility is not always such a wonderful thing -- if you're using the return value, you still have to pass NULL as a pointer:

再后来,有人意识到如果time()继续返回值会很有用,而不仅仅是通过指针填充它。但是——向后兼容性是一件很棒的事情——为了所有仍在执行的代码的利益time(&now),该time()函数必须继续支持指针参数。这就是为什么——这就是为什么向后兼容性并不总是那么好的事情——如果你使用返回值,你仍然必须将 NULL 作为指针传递:

long now = time(NULL);

(Later still, of course, we started using time_tinstead of plain longfor times, so that, for example, it can be changed to a 64-bit type, dodging the y2.038k problem.)

(当然,后来我们开始多次使用time_t而不是普通long的,这样,例如,可以将其更改为 64 位类型,从而避免 y2.038k 问题。)

[P.S. I'm not actually sure the change from int [2]to long, 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 hadto be filled in via a pointer, it couldn't be returned as a value, because of course C functions can't return arrays.]

[PS 我实际上不确定从int [2]long的更改以及添加返回值的更改发生在不同的时间;它们可能同时发生。但是要注意,当时间表示为数组时,必须通过指针来填充,不能作为值返回,因为C函数当然不能返回数组。]

回答by Mob

Time: It returns the time elapsed in seconds since the epoch 1 Jan 1970

Time:它返回自 1970 年 1 月 1 日以来经过的时间(以秒为单位)

回答by jwodder

The timefunction returns the current time (as a time_tvalue) in seconds since some point (on Unix systems, since midnight UTC January 1, 1970), and it takes one argument, a time_tpointer in which the time is stored. Passing NULLas the argument causes timeto return the time as a normal return value but not store it anywhere else.

time函数time_t以秒为单位返回自某个时间点(在 Unix 系统上,自 UTC 时间 1970 年 1 月 1 日午夜起)以来的当前时间(作为一个值),它接受一个参数,即一个time_t存储时间的指针。NULL作为参数传递会导致time将时间作为正常返回值返回,但不会将其存储在其他任何地方。

回答by k_clark.90

int main (void)
{   
    //print time in seconds from 1 Jan 1970 using c   
    float n = time(NULL);   
    printf("%.2f\n" , n);      
}      

this prints 1481986944.00 (at this moment).

这会打印 1481986944.00(此时)。

回答by Mahmoud Al-Qudsi

You have to refer to the documentation for ctime. timeis a function that takes one parameter of type time_t *(a pointer to a time_tobject) and assigns to it the current time. Instead of passing this pointer, you can also pass NULLand then use the returned time_t value instead.

您必须参考ctime文档time是一个函数,它接受一个类型的参数time_t *(指向time_t对象的指针)并将当前时间分配给它。除了传递这个指针,您还可以传递NULL然后使用返回的 time_t 值。

回答by Md. Al Amin Bhuiyan

You can pass in a pointer to a time_tobject that time will fill up with the current time (and the return value is the same one that you pointed to). If you pass in NULL, it just ignores it and merely returns a new time_tobject that represents the current time.

您可以传入一个指向time_t对象的指针,该对象的时间将填满当前时间(并且返回值与您指向的值相同)。如果你传入NULL,它只是忽略它,只返回一个time_t代表当前时间的新对象。

Nb:time(&timer);is equivalent to timer = time(NULL);

Nb:time(&timer);相当于 timer = time(NULL);