我有一个关于glibc ctime()如何工作的问题。
下面是我的代码片段:
下面是我的代码片段:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main (int argc,char** argv)
{
int ret=EXIT_SUCCESS;
time_t tm1;
time_t tm2;
tm1 = time(NULL);
tm2 = tm1 + 60; // 60 seconds later
puts("1st method-------");
printf("tm1 = %stm2 = %s",ctime(&tm1),ctime(&tm2));
puts("2nd method-------");
printf("tm1 = %s",ctime(&tm1));
printf("tm2 = %s",ctime(&tm2));
return(ret);
}
I got:
1st method-------
tm1 = Sat Jan 14 01:13:28 2012
tm2 = Sat Jan 14 01:13:28 2012
2nd method-------
tm1 = Sat Jan 14 01:13:28 2012
tm2 = Sat Jan 14 01:14:28 2012
正如您所看到的,第一种方法中两个时间都有相同的值,这是不正确的。在第二种方法中,我获得了正确的值。
我知道ctime()将这些字符串放在静态缓冲区中,并且要覆盖它,我们需要连续调用ctime()。
问题:在第一种方法中我没有进行连续调用吗?
谢谢您的回复。
C
中,函数参数的求值顺序没有定义。你可能已经知道了这一点。 https://dev59.com/ZnRC5IYBdhLWcg3wMeLg - VoidPointer