c&时间问题 [英] c & time problem
问题描述
我在理解这段代码的工作原理时遇到了问题。
据我所知,ctime function返回一个字符串。主要是
返回string分配给一个整数(16位)并在MAIN中用
a字符串格式打印。
有人会解释这是如何工作和/或纠正我的理解吗? />
#include< stdio.h>
#include< time.h>
//获取系统日期和时间
int main(无效)
{
int i;
i = mytim();
printf(D / T =%s,i);
}
mytim()
{
time_t now;
if((now = time(NULL))==(time_t) - 1)
puts("失败:无法获得时间);
else
printf(" Date& time:%s",ctime(& now));
//下列任何一项都同样有效
return(ctime(& now));
}
RVO写道:
我有一个问题的理解为什么这段代码有效。
As我理解为ctime function返回一个字符串。主要是
返回string被赋值为一个整数(16位),并在MAIN中以字符串格式打印
。
为什么假设16位int?函数名称是main,而不是MAIN。
有人会详细解释这是如何工作的和/或纠正我的理解吗?
#include< stdio.h>
#include< time.h>
//获取系统日期和时间
int main(无效)
{
int i;
i = mytim();
printf(" D / T =%s",i);
}
mytim()
不要依赖隐式int返回类型。
{
time_t now;
if((now = time(NULL))==(time_t) - 1)
puts(FAIL:无法获得时间);
else
printf(" Date& time:%s",ctime(& now));
//以下任何一项都同样有效
返回(ctime(& now));
}
您是否注意到所有编译器警告当你编译这个时,会发出什么?
未定义行为的经典案例,sizeof char *碰巧等于
sizeof int 。
-
Ian Collins。
Ian Collins写道:
< blockquote class =post_quotes>
> RVO写道:
>>
我有一个问题理解为什么这个代码有效。
据我所知,ctime function返回一个字符串。主要是
返回的字符串分配给一个整数(16位)并在MAIN中打印一个字符串格式。
为什么假设16位int?函数名称是main,而不是MAIN。
1)因为它是。
2)MAIN强调。
>
>>有人会解释这是如何工作和/或纠正我的<理解?
#include< stdio.h>
#include< time.h>
//获取系统日期和时间
int main (无效)
{
int i;
i = mytim();
printf(" D / T =%s",i);
}
mytim()
不要依赖隐式的int返回类型。
3)从C获得代码网络专家。
>
>> {
time_t now;
if((now = time(NULL))==(time_t) - 1)
puts(FAIL:Unable get time);
printf( 日期&时间:%s,ctime(& now));
//以下任何一项都能很好地工作
return(ctime(& now));
}
你是否注意到编译它时发出的所有编译器警告?
4)就我所知,警告与此无关。
>未定义行为的经典案例,sizeof char *碰巧等于
sizeof int。
5)但是如果C是是如此独特为什么它没有用
整数标记%s的使用?
RVO写道:
Ian Collins写道:
> RVO写道:
>>
我在理解这段代码的工作原理时遇到了问题。
正如我所理解的那样,ctime function返回一个字符串。主要是
返回的字符串分配给一个整数(16位)并在MAIN中用字符串格式打印。
为什么假设16位int?函数名称是main,而不是MAIN。
1)因为它是。
什么?
>>
不要依赖隐式int返回类型。
3)从C获得代码网络专家。
一个可以避免的。
> ;>
你是否注意到编译它时发出的所有编译器警告?
4)据我所知,警告与此无关。
/ tmp / xc:在函数main中:
/ tmp / xc:7:警告:隐式声明函数`mytim''
/ tmp / xc:8:警告:格式参数不是指针(arg 2)
/ tmp / xc:9:警告:控制到达无效功能的结束
/ tmp / xc:在顶级:
/ tmp / xc:12:警告:返回类型默认为int'
/ tmp / xc:在函数mytim中:
/ tmp / xc:19:警告:返回从指针中生成整数而没有强制转换
哪一个?第8行的警告看起来很明显。也许你没有在符合模式下调用你的编译器。
>经典案例对于未定义的行为,sizeof char *恰好等于
sizeof int。
5)但是如果C是是如此独特为什么它没有用
整数标记%s的用法?
gcc做了,见上文。
-
Ian Collins。
Hi,
I am having a problem understanding why this code works.
As I understand it the "ctime" function returns a string. In main the
returned "string" is assigned to an integer (16 bit) and is printed with
a string format in MAIN.
Would someone kindly explain how this works and/or correct my understanding?
#include <stdio.h>
#include <time.h>
// Gets System Date and Time
int main(void)
{
int i;
i = mytim();
printf("D/T= %s", i);
}
mytim()
{
time_t now;
if( (now = time(NULL)) == (time_t) - 1 )
puts("FAIL: Unable to get time");
else
printf("Date & time: %s", ctime(&now));
// Either of following works equally well
return (ctime(&now));
}
RVO wrote:Hi,
I am having a problem understanding why this code works.
As I understand it the "ctime" function returns a string. In main the
returned "string" is assigned to an integer (16 bit) and is printed with
a string format in MAIN.Why do you assume 16 bit int? The function name is main, not MAIN.
Would someone kindly explain how this works and/or correct my
understanding?
#include <stdio.h>
#include <time.h>
// Gets System Date and Time
int main(void)
{
int i;
i = mytim();
printf("D/T= %s", i);
}
mytim()Don''t rely on implicit int return type.
{
time_t now;
if( (now = time(NULL)) == (time_t) - 1 )
puts("FAIL: Unable to get time");
else
printf("Date & time: %s", ctime(&now));
// Either of following works equally well
return (ctime(&now));
}Did you take note of all of the compiler warnings emitted when you
compiled this?
A classic case of undefined behaviour, sizeof char* happens to equal
sizeof int.
--
Ian Collins.
Ian Collins wrote:
>RVO wrote:
>>Hi,
I am having a problem understanding why this code works.
As I understand it the "ctime" function returns a string. In main the
returned "string" is assigned to an integer (16 bit) and is printed with
a string format in MAIN.
Why do you assume 16 bit int? The function name is main, not MAIN.
1) because it is.
2) MAIN for emphasis.
>
>>Would someone kindly explain how this works and/or correct my
understanding?
#include <stdio.h>
#include <time.h>
// Gets System Date and Time
int main(void)
{
int i;
i = mytim();
printf("D/T= %s", i);
}
mytim()
Don''t rely on implicit int return type.
3) Got code from "C" expert on web.
>
>>{
time_t now;
if( (now = time(NULL)) == (time_t) - 1 )
puts("FAIL: Unable to get time");
else
printf("Date & time: %s", ctime(&now));
// Either of following works equally well
return (ctime(&now));
}
Did you take note of all of the compiler warnings emitted when you
compiled this?
4) the warning was not related to this as far as I can tell.
>A classic case of undefined behaviour, sizeof char* happens to equal
sizeof int.
5) But if "C" is so unique why does it not flag the %s usage with the
integer?
RVO wrote:Ian Collins wrote:
>RVO wrote:
>>Hi,
I am having a problem understanding why this code works.
As I understand it the "ctime" function returns a string. In main the
returned "string" is assigned to an integer (16 bit) and is printed with
a string format in MAIN.
Why do you assume 16 bit int? The function name is main, not MAIN.
1) because it is.
On what?
>>
Don''t rely on implicit int return type.
3) Got code from "C" expert on web.
One to avoid then.
>>
Did you take note of all of the compiler warnings emitted when you
compiled this?
4) the warning was not related to this as far as I can tell.
/tmp/x.c: In function `main'':
/tmp/x.c:7: warning: implicit declaration of function `mytim''
/tmp/x.c:8: warning: format argument is not a pointer (arg 2)
/tmp/x.c:9: warning: control reaches end of non-void function
/tmp/x.c: At top level:
/tmp/x.c:12: warning: return type defaults to `int''
/tmp/x.c: In function `mytim'':
/tmp/x.c:19: warning: return makes integer from pointer without a cast
Which one? The warning for line 8 looks plain enough. Perhaps you
didn''t invoke your compiler in conforming mode.
>A classic case of undefined behaviour, sizeof char* happens to equal
sizeof int.
5) But if "C" is so unique why does it not flag the %s usage with the
integer?gcc did, see above.
--
Ian Collins.
这篇关于c&时间问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!