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

发布于 2024-12-06 05:37:49 字数 76 浏览 1 评论 0原文

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

这究竟意味着什么?

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

What exactly does this mean?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

相对绾红妆 2024-12-13 05:37:49

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

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 in NULL, it just ignores it and merely returns a new time_t object that represents the current time.

悟红尘 2024-12-13 05:37:49

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 a time_t variable, that pointer variable will point to the current time.

花伊自在美 2024-12-13 05:37:49

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

因此,您调用

int now[2];
time(now);

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

稍后,dmr 完成了向编译器添加 long ,所以你可以开始说

long now;
time(&now);

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

long 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 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.

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 localtime and ctime, tend to accept their time arguments via pointers, too.)

Later on, dmr finished adding long to the compiler, so you could start saying

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, after all — if you're using the return value, you still have to pass NULL as a pointer:

long now = time(NULL);

(Later still, of course, we started using time_t instead of plain long 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] 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 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.]

玩世 2024-12-13 05:37:49

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

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

笑饮青盏花 2024-12-13 05:37:49

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

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

︶葆Ⅱㄣ 2024-12-13 05:37:49

您必须参考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 type time_t * (a pointer to a time_t object) and assigns to it the current time. Instead of passing this pointer, you can also pass NULL and then use the returned time_t value instead.

失眠症患者 2024-12-13 05:37:49

您可以传入一个指向 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 in NULL, it just ignores it and merely returns a new time_t object that represents the current time.

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

爱的十字路口 2024-12-13 05:37:49
int main (void)
{   
    //print time in seconds from 1 Jan 1970 using c   
    float n = time(NULL);   
    printf("%.2f\n" , n);      
}      

这将打印 1481986944.00(此时)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文