【问题标题】:How does the ctime function in this program work?这个程序中的 ctime 函数是如何工作的?
【发布时间】:2013-10-03 09:08:06
【问题描述】:

我正在发布一个我遇到的程序。谁能解释一下

  1. “0x7FFFFFFF”是什么意思?
  2. ctime() 函数如何工作?

#include<stdio.h>
#include <time.h> 

int main()
{ 
     time_t Variable = 0x7FFFFFFF; 
     printf("Variable value is = %s \n", ctime(&Variable) ); 
     return 0; 
} 

【问题讨论】:

    标签: c ctime time.h


    【解决方案1】:

    ctimetime_t 值转换为字符串。来自Wiki

    time_t 作为算术类型,但没有指定任何特定类型

    0x7FFFFFFF 是十进制的2147483647,等于 2^31 - 1。可以用 32 位有符号整数表示的最大值。

    【讨论】:

      【解决方案2】:

      0x7FFFFFFF 是可以用 32 位有符号整数表示的最大值。如果time_t 是有符号整数类型,那么ctime(&amp;Variable) 代表32 位系统上的end of the world。我们将从那里进入未定义的世界。 ;-)

      但是,end 已经 postponed 使用了 64 位类型的 time_t

      【讨论】:

        【解决方案3】:

        ctime 通常按如下方式使用

        /* ctime example */
        #include <stdio.h>      /* printf */
        #include <time.h>       /* time_t, time, ctime */
        
        int main ()
        {
          time_t rawtime;
        
          time (&rawtime);
          printf ("The current local time is: %s", ctime (&rawtime));
        
          return 0;
        }
        

        我们首先使用 time() 将当前时间保存在 rawtime 变量中,然后使用 ctime() 将其显示为人类可读的字符串

        【讨论】:

          猜你喜欢
          • 2012-08-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-07-17
          • 2016-02-22
          • 1970-01-01
          相关资源
          最近更新 更多