time()
函数在 C 语言中用于获取当前日历时间,返回值为自纪元(Epoch,1970年1月1日 00:00:00 UTC)以来的秒数。这个秒数通常以 time_t
类型表示。
time()
函数原型如下:
#include <time.h>
time_t time(time_t* timer);
参数:
timer
:一个指向 time_t
类型变量的指针,用于存储当前时间。如果传递的是 NULL
,则不会将结果存储在任何地方。返回值:
time_t
类型)。(time_t)(-1)
。示例:
#include<stdio.h>
#include <time.h>
int main() {
time_t currentTime;
currentTime = time(NULL);
printf("当前时间戳为:%ld\n", (long)currentTime);
return 0;
}
在这个示例中,我们使用 time()
函数获取当前时间戳,并将其打印到控制台。注意,我们将 time_t
类型转换为 long
类型以确保正确打印。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>