为什么取得的两个时间是一样的?

宝码 2005-10-17 12:53:07
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(int argc, char *argv[])
{
int a =1127969120;
int b =1127969137;
printf("%s%s\n",ctime(&a),ctime(&b));
system("PAUSE");
return 0;
}
...全文
90 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaocai0001 2005-10-17
  • 打赏
  • 举报
回复
不小心按上回车了, 竟然发了回帖.
对不起楼主.

关于两个时间是一样的

你在一个语句里执行两个取时间动作, 程序执行速度有多快啊, 你怎么能期望它不一样呢?
xiaocai0001 2005-10-17
  • 打赏
  • 举报
回复
q
kunp 2005-10-17
  • 打赏
  • 举报
回复
和线程安全有关,ctime是非线程安全的,返回值应该是一个全局的指针,所以
ctime(&a)和ctime(&b)其实是同一个指针,放在一起打印,结果当然一样,因为值变成最后一次执行ctime的值了,代码改成这样可以看出问题

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

int main(int argc, char *argv[])
{
int a =1127969120;
int b =1127969137;
char t1[1024];
char t2[1024];
strcpy( t1, ctime(&a) );
strcpy( t2, ctime(&b) );
printf("%s\n%s\n" , t1, t2);
system("PAUSE");
return 0;
}
chenhu_doc 2005-10-17
  • 打赏
  • 举报
回复
问楼主是用的什么编译器》 borland tc ??
chenhu_doc 2005-10-17
  • 打赏
  • 举报
回复
可以向你想的那样去做,毕竟这是一个单独的测试程序了!
chenhu_doc 2005-10-17
  • 打赏
  • 举报
回复
#include <time.h>
#include <stdio.h>

void main( void )
{
time_t ltime;

time( <ime );
printf( "The time is %s\n", ctime( <ime ) );
}

宝码 2005-10-17
  • 打赏
  • 举报
回复
一个是取a,一个是取b,为什么一样?
难道我要程序等一会儿再取?

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧