我搜索我的Linux盒子,看到这样的类型定义:
typedef __time_t time_t;
但是我找不到__time_t的定义。
我搜索我的Linux盒子,看到这样的类型定义:
typedef __time_t time_t;
但是我找不到__time_t的定义。
当前回答
标准
威廉·布伦德尔引用了维基百科,但我更喜欢它来自马的嘴。
C99 N1256标准草案7.23.3 /3“时间组件”说:
声明的类型是size_t(在7.17中描述)clock_t和time_t,它们是能够表示时间的算术类型
6.2.5/18“类型”说明:
整型和浮动类型统称为算术类型。
POSIX 7 sys_types.h说:
[CX] time_t为整数类型。
其中[CX]定义为:
[CX] ISO C标准的扩展。
它是一种扩展,因为它提供了更强有力的保证:浮点数过时了。
gcc一行程序
不需要像Quassnoi提到的那样创建一个文件:
echo | gcc -E -xc -include 'time.h' - | grep time_t
在Ubuntu 15.10 GCC 5.2中,前两行是:
typedef long int __time_t;
typedef __time_t time_t;
命令分解和来自man gcc的一些引用:
-E:“预处理阶段结束后停止;不要正确运行编译器。” -xc:指定C语言,因为输入来自stdin,没有文件扩展名。 -include file: "处理文件,如果"#include "file""出现为主源文件的第一行。" -: stdin输入
其他回答
[根]#猫时间
#include <time.h>
int main(int argc, char** argv)
{
time_t test;
return 0;
}
[root]# gcc -E time.c | grep __time_t .c
类型定义long int __time_t;
它在$INCDIR/bits/types.h中定义:
# 131 "/usr/include/bits/types.h" 3 4
# 1 "/usr/include/bits/typesizes.h" 1 3 4
# 132 "/usr/include/bits/types.h" 2 3 4
time_t维基百科的文章给出了一些解释。归根结底,在C规范中,time_t的类型是不保证的。
The time_t datatype is a data type in the ISO C library defined for storing system time values. Such values are returned from the standard time() library function. This type is a typedef defined in the standard header. ISO C defines time_t as an arithmetic type, but does not specify any particular type, range, resolution, or encoding for it. Also unspecified are the meanings of arithmetic operations applied to time values. Unix and POSIX-compliant systems implement the time_t type as a signed integer (typically 32 or 64 bits wide) which represents the number of seconds since the start of the Unix epoch: midnight UTC of January 1, 1970 (not counting leap seconds). Some systems correctly handle negative time values, while others do not. Systems using a 32-bit time_t type are susceptible to the Year 2038 problem.
在64位机器上,Time_t的类型为long int,否则为long long int。
你可以在这些头文件中验证这一点:
time.h: /usr/include Types.h和typeszes .h: /usr/include/x86_64-linux-gnu/bits
(下面的陈述不是一个接一个的。他们可以在答复中找到。头文件使用Ctrl+f搜索。)
1)在time.h
typedef __time_t time_t;
2)在types.h
# define __STD_TYPE typedef
__STD_TYPE __TIME_T_TYPE __time_t;
3)在typesizes.h
#define __TIME_T_TYPE __SYSCALL_SLONG_TYPE
#if defined __x86_64__ && defined __ILP32__
# define __SYSCALL_SLONG_TYPE __SQUAD_TYPE
#else
# define __SYSCALL_SLONG_TYPE __SLONGWORD_TYPE
#endif
4)再次在types.h
#define __SLONGWORD_TYPE long int
#if __WORDSIZE == 32
# define __SQUAD_TYPE __quad_t
#elif __WORDSIZE == 64
# define __SQUAD_TYPE long int
#if __WORDSIZE == 64
typedef long int __quad_t;
#else
__extension__ typedef long long int __quad_t;
标准
威廉·布伦德尔引用了维基百科,但我更喜欢它来自马的嘴。
C99 N1256标准草案7.23.3 /3“时间组件”说:
声明的类型是size_t(在7.17中描述)clock_t和time_t,它们是能够表示时间的算术类型
6.2.5/18“类型”说明:
整型和浮动类型统称为算术类型。
POSIX 7 sys_types.h说:
[CX] time_t为整数类型。
其中[CX]定义为:
[CX] ISO C标准的扩展。
它是一种扩展,因为它提供了更强有力的保证:浮点数过时了。
gcc一行程序
不需要像Quassnoi提到的那样创建一个文件:
echo | gcc -E -xc -include 'time.h' - | grep time_t
在Ubuntu 15.10 GCC 5.2中,前两行是:
typedef long int __time_t;
typedef __time_t time_t;
命令分解和来自man gcc的一些引用:
-E:“预处理阶段结束后停止;不要正确运行编译器。” -xc:指定C语言,因为输入来自stdin,没有文件扩展名。 -include file: "处理文件,如果"#include "file""出现为主源文件的第一行。" -: stdin输入
在大多数传统平台上,它是一个32位有符号整数类型。然而,这会导致您的代码遭受2038年的错误。因此,现代C库应该将其定义为有符号的64位整型,这在几十亿年里是安全的。