ctime双重双重检查 [英] ctime double double check

查看:65
本文介绍了ctime双重双重检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在使用ctime转换以下字符串1144412677847 ....

如果他们得到相同的话,可以请一些人仔细检查结果为

me(The Time is Sun Nov 02 09:11:51 2031)。这似乎不正确,

想要第二个opion。


#include< time.h>

#include< stdio.h>


int main()

{

unsigned long时间戳;

time_t CTime;

时间戳=(长)1144412677847;

CTime = ctime(& Timestamp);

printf(&Time Time s",CTime);

退出(0);

}


时间是太阳11月02日09:11: 51 2031

解决方案

3月8日上午9:28,aisling.cro ... @ gmail.com写道:





我正在使用ctime转换以下字符串1144412677847 ....

Please如果他们得到与我相同的结果(时间是太阳11月02日09:11:51 2031),可能有人要仔细检查一下。这似乎是不正确的,

想要第二个opion。



首先,你的示例代码是错误的。在继续之前修复它。

其次,你的整数常量可能太大而无法保留未签名的长期

(或时间_t,见下文)。检查编译器警告。


#include< time.h>

#include< stdio.h>


int main()



ITYM

int main(无效)


{

unsigned long时间戳;

time_t CTime;

时间戳=(长)1144412677847;



演员不会帮忙;删除它。

由于时间戳被定义为unsigned long,你的演员可能会降低值的精确度(假设常量可以适合未签名的

长,但不是签名长)。它肯定不会增加该值的精确度。


CTime = ctime(& Timestamp);



方式错误。


去查看ctime()调用,并重读文档

1)ctime()返回一个指向char的指针,而不是time_t值

2)ctime()将唯一的参数作为指向time_t的指针,而不是

指向unsigned long的指针(time_t可能/或者可能不是/或者类型为
这样)


printf(" ;时间是%s,CTime);



错误的方法。


查看printf()调用并重新阅读文档。

具体来说,看看每个转义对应的参数类型

序列。


你使用%s,它对应一个char *,但你给它一个time_t

而不是。


退出(0);


}


时间是太阳11月02日09:11:51 2031



ai ************ @ gmail.com 写道:


#include< time.h>

#include< stdio.h>


int main ()

{

无符号长时间戳;

time_t CTime;

时间戳=(长)1144412677847;

CTime = ctime(& Timestamp);



ctime()获取指向time_t的指针,而不指向unsigned long;并且它

返回一个char *,而不是time_t。

即使time_t恰好是相同大小等等

unsigned很长,这并不意味着任何随机积分值分配给一个人将是一个有效的时间。时间的编码方式是

time_t是系统相关的;

所有的ISO C标准都没有要求它,除了它是一种算术类型(以某种方式)代表一个

时间。您的大数字可能代表或不代表您认为它在您的系统上代表
代表的时间; ISO C,因此comp.lang.c,不能
告诉。


printf(时间是%s,CTime );



HTF你是否设法将其编译好?


exit(0);



你需要< stdlib.hfor退出()。


Richard


ai ************ @ gmail.com 写于03/08/07 09:28,:





我正在使用ctime来转换以下字符串1144412677847 ....

如果他们得到的结果与

我相同,那么可以请一两个人仔细查看(时间是太阳11月02日09:11:51 2031)。这似乎不正确,

想要第二个opion。


#include< time.h>

#include< stdio.h>


int main()

{

unsigned long时间戳;

time_t CTime;

时间戳=(长)1144412677847;

CTime = ctime(& Timestamp);



获取不同的编译器。一个工作的C编译器应该

在这里发出至少一个诊断,可能是两个(或者更多b
),因为这个语句严重错误:


1)ctime()的参数必须是'time_t *'',而不是

你提供的`unsigned long *''。在你正在使用的系统上有一个微弱的

可能性,unsigned long和time_t是同义词

,因此很有可能

不匹配毕竟是比赛。尽管如此,这是一个错误,

,因为只要你将代码移动到一台机器,其中

`time_t''不是'unsigned long''(并且有很多很多)

这样的机器),代码错了,RONG,错了。


2)ctime()返回的值是'char *'' ,

*保证*与'time_t''不一样。然而你要
尝试将这个'char *''值存储在'time_t''变量中。

需要一个合适的编译器来抱怨这种滥用行为。


printf(" The Time is%s",CTime);



这是另一个错误:%s转换必须是

与'char *''参数匹配,但你提供的是

`time_t''。编译器不需要捕获

这个错误,但好的编译器无论如何都会发现它。获得

其中一个。


exit(0);



又一个错误,尽管是静脉一。

范围内没有exit()函数的声明,所以

编译器会认为它需要一个固定但未知的数字

of参数并返回一个int值。实际上,

exit()接受一个参数并且不返回任何值,因此在调用点处的

假设不符合现实。

这是未定义的行为 - UB你可能会贬值,但是U.B.尽管如此。


}


时间是太阳11月02日09:11:51 2031


-
Er ********* @ sun .com


Hi

I am using ctime to convert the following string 1144412677847 ....
Please could some one to double check if they get the same result as
me (The Time is Sun Nov 02 09:11:51 2031). It seems incorrect and
would like a second opion.

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

int main ()
{
unsigned long Timestamp;
time_t CTime;
Timestamp= (long)1144412677847 ;
CTime=ctime(&Timestamp);
printf ("The Time is %s", CTime);
exit(0);
}

The Time is Sun Nov 02 09:11:51 2031

解决方案

On Mar 8, 9:28 am, aisling.cro...@gmail.com wrote:

Hi

I am using ctime to convert the following string 1144412677847 ....
Please could some one to double check if they get the same result as
me (The Time is Sun Nov 02 09:11:51 2031). It seems incorrect and
would like a second opion.

First off, your example code is wrong. Fix it before you continue.
Secondly, your integer constant may be too large for an unsigned long
(or a time_t, see below) to hold. Check your compiler warnings.

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

int main ()

ITYM
int main(void)

{
unsigned long Timestamp;
time_t CTime;
Timestamp= (long)1144412677847 ;

The cast wont help; remove it.
As Timestamp is defined as unsigned long, your cast may reduce the
precision of the value (assume the constant can fit into an unsigned
long, but not a signed long). It certainly wont increase the precision
of the value.

CTime=ctime(&Timestamp);

Way wrong.

go look up the ctime() call, and reread the documentation
1) ctime() returns a pointer to a char, not time_t value
2) ctime() takes as its only argument a pointer to a time_t, not a
pointer to an unsigned long (time_t may /or may not/ be typedefed as
such)

printf ("The Time is %s", CTime);

Way wrong.

Go look up the printf() call and reread the documentation.
Specifically, look at what argument types correspond to each escape
sequence.

You use "%s", which corresponds to a char *, but you give it a time_t
instead.

exit(0);

}

The Time is Sun Nov 02 09:11:51 2031



ai************@gmail.com wrote:

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

int main ()
{
unsigned long Timestamp;
time_t CTime;
Timestamp= (long)1144412677847 ;
CTime=ctime(&Timestamp);

ctime() takes a pointer to a time_t, not to an unsigned long; and it
returns a char *, not a time_t.
Even where time_t happens to be the same size and so forth as an
unsigned long, that doesn''t mean that any random integral value assigned
to one is going to be a valid time. The way times are encoded in a
time_t is system-dependent; the ISO C Standard makes no demands on it at
all, beyond it being an arithmetic type that (somehow) represents a
time. Your big number may or may not represent the time you think it
represents, on your system; ISO C, and therefore comp.lang.c, cannot
tell.

printf ("The Time is %s", CTime);

HTF did you manage to get this compiled?

exit(0);

You need <stdlib.hfor exit().

Richard


ai************@gmail.com wrote On 03/08/07 09:28,:

Hi

I am using ctime to convert the following string 1144412677847 ....
Please could some one to double check if they get the same result as
me (The Time is Sun Nov 02 09:11:51 2031). It seems incorrect and
would like a second opion.

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

int main ()
{
unsigned long Timestamp;
time_t CTime;
Timestamp= (long)1144412677847 ;
CTime=ctime(&Timestamp);

Get a different compiler. A working C compiler should
have issued at least one diagnostic here, perhaps two (or
even more), because this statement is grossly in error:

1) The argument to ctime() must be a `time_t*'', not
the `unsigned long*'' you have provided. There is a faint
possibility that `unsigned long'' and `time_t'' are synonyms
on the system you are using, hence a faint chance that the
"mismatch" matches after all. It is an error nonetheless,
because as soon as you move the code to a machine where
`time_t'' is not `unsigned long'' (and there are manymanymany
such machines), the code will be wrong, R-O-N-G, wrong.

2) The value returned by ctime() is a `char*'', which
is *guaranteed* not to be the same as a `time_t''. Yet you
try to store this `char*'' value in a `time_t'' variable.
A proper compiler is required to complain about this abuse.

printf ("The Time is %s", CTime);

This is another error: The "%s" conversion must be
matched with a `char*'' argument, yet you are supplying a
`time_t'' instead. The compiler is not required to catch
this error, but good compilers will spot it anyhow. Get
one of them.

exit(0);

Yet another error, albeit a "venial" one. There is
no declaration of the exit() function in scope, so the
compiler will assume it takes a fixed but unknown number
of arguments and returns an `int'' value. In actual fact,
exit() takes one argument and returns no value, so the
assumption at the point of call disagrees with reality.
It''s undefined behavior -- U.B. that you are likely to
get away with, but U.B. nonetheless.

}

The Time is Sun Nov 02 09:11:51 2031


--
Er*********@sun.com


这篇关于ctime双重双重检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆