time()
函数返回自1970年1月1日0点以来经过的秒数,每秒变化一次?
time()
函数定义在头文件<time.h>
中,原型是:
time_t time(time_t *arg);
如果arg
不是空指针,那么函数返回time_t
类型的calendar time,并且把结果保存在arg
指向的对象;
如果arg == NULL
,那么函数只是返回一个值,值不能存储在空指针指向的对象。
之前不明白为什么要设计一个参数arg
,直接返回一个值就好了啊?
有大神说,这是因为:
很久很久以前,据说
time_t
是个struct
,那时候c语言不支持函数返回struct
,所以只能用指针传进去。
那么time_t
到底是什么类型呢?
看看cppreference.com的定义:
The encoding of calendar time in
time_t
is unspec