time_t的类型

在毕设中用到了time_t的类型,但是发现在types.h里面没有找到它的具体定义,只知道是8个字节长度,于是
写了下面这段测试代码:

#include 
#include 
#include 
int main(int argc,char *argv[])
{
    u_int64_t s1=(1<<31);
    int64_t s2=(1<<31);
    time_t  s3=(1<<31);
    printf("sizeof(s1)=%ld,s1>0? %s\n",sizeof(s1),s1>0?"true":"false");
    printf("sizeof(s2)=%ld,s2>0? %s\n",sizeof(s2),s2>0?"true":"false");
    printf("sizeof(s3)=%ld,s3>0? %s\n",sizeof(s3),s3>0?"true":"false");
    return 0;
}

输出如下:

sizeof(s1)=8,s1>0? true
sizeof(s2)=8,s2>0? false
sizeof(s3)=8,s3>0? false

所以可以知道time_t的类型是8字节的有符号整数。



time.h中,time_t是这样定义的:

// 在cdefs.h
//# define __USING_NAMESPACE_STD(name) using std::name;

__BEGIN_NAMESPACE_STD
/* Returned by `time'.  */
typedef __time_t time_t;
__END_NAMESPACE_STD
#ifdef __USE_POSIX
__USING_NAMESPACE_STD(time_t)
#endif

/usr/include/x86_64-linux-gnu/bits/types.h中:

# define __STD_TYPE		typedef
__STD_TYPE __TIME_T_TYPE __time_t;	/* Seconds since the Epoch.  */

/usr/include/x86_64-linux-gnu/bits/typesizes.h中:

/* X32 kernel interface is 64-bit.  */
#if defined __x86_64__ && defined __ILP32__
# define __SYSCALL_SLONG_TYPE	__SQUAD_TYPE
# define __SYSCALL_ULONG_TYPE	__UQUAD_TYPE
#else
# define __SYSCALL_SLONG_TYPE	__SLONGWORD_TYPE
# define __SYSCALL_ULONG_TYPE	__ULONGWORD_TYPE
#endif
#define __TIME_T_TYPE		__SYSCALL_SLONG_TYPE

/usr/include/x86_64-linux-gnu/bits/types.h

/* quad_t is also 64 bits.  */
#if __WORDSIZE == 64
typedef long int __quad_t;
typedef unsigned long int __u_quad_t;
#else
__extension__ typedef long long int __quad_t;
__extension__ typedef unsigned long long int __u_quad_t;
#endif
#define __SLONGWORD_TYPE	long int
#if __WORDSIZE == 32
# define __SQUAD_TYPE		__quad_t
#elif __WORDSIZE == 64
# define __SQUAD_TYPE		long int
#else
# error
#endif

/usr/include/x86_64-linux-gnu/bits/wordsize.h中:

#if defined __x86_64__ && !defined __ILP32__
# define __WORDSIZE	64
#else
# define __WORDSIZE	32
#endif

/usr/include/x86_64-linux-gnu/sys/cdefs.h中:

/* It is possible to compile containing GCC extensions even if GCC is
   run in pedantic mode if the uses are carefully marked using the
   `__extension__' keyword.  But this is not generally available before
   version 2.8.  */
#if !__GNUC_PREREQ (2,8)
# define __extension__		/* Ignore */
#endif

/usr/include/features.h中:

/* Convenience macros to test the versions of glibc and gcc.
   Use them like this:
   #if __GNUC_PREREQ (2,8)
   ... code requiring gcc 2.8 or later ...
   #endif
   Note - they won't work for gcc1 or glibc1, since the _MINOR macros
   were not defined then.  */
#if defined __GNUC__ && defined __GNUC_MINOR__
# define __GNUC_PREREQ(maj, min) \
	((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
#else
# define __GNUC_PREREQ(maj, min) 0
#endif

你可能感兴趣的:(调试经验)