time()函数在time.h(在C++中为ctime)头文件中定义。此函数以秒为单位返回自1970年1月1日UTC 00:00:00(Unix时间戳)以来的时间。如果second不是null指针,则返回的值也存储在second指向的对象中。
句法:
time_t time( time_t *second )
参数:该函数接受单个参数second 。此参数用于设置存储时间的time_t对象。
返回值:该函数将当前的压延时间作为类型为time_t的对象返回。
程序1:
// C program to demonstrate
// example of time() function.
#include
#include
int main ()
{
time_t seconds;
seconds = time(NULL);
printf("Seconds since January 1, 1970 = %ld\n", seconds);
return(0);
}
输出:
Seconds since January 1, 1970 = 1538123990
范例2:
// C program to demonstrate
// example of time() function.
#include
#include
int main()
{
time_t seconds;
// Stores time seconds
time(&seconds);
printf("Seconds since January 1, 1970 = %ld\n", seconds);
return 0;
}
输出:
Seconds since January 1, 1970 = 1538123990
想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。