第1个回答 2012-04-26
废话少说!对于CLOCKS_PER_SEC()简单地理解就是用来计算程序本身的执行时间
以下程序可以验证如:
#include
<stdio.h>
#include
<time.h>
#include
<windows.h>
void
main()
{
Sleep(1000);//让程序休眠一秒钟
printf("Elapsed
time:
%u
secs.\n",
clock()/CLOCKS_PER_SEC);
}
//很显然从本程序来说主函数体里用到了Sleep函数并且让它休眠了一秒钟,所以这个程序执行的时间就是1秒
第2个回答 2008-04-04
C语言的建时间函数是
mktime(),原型在
里
调用有点繁。
下面,用我的程序输入
年月日时分秒,调用mktime(),
就得
C语言
可直接使用的
时间,
存放在
t
里。
例如
输入年月日时分秒:
2008
8
16
9
55
25
time_t
t;
里
就有了
各种时间信息,例如星期几...
#include
#include
void
main(){
struct
tm
*target_time;
time_t
rawtime,
t;
int
year,month,mday,hh,mm,ss;
time
(
&rawtime
);
target_time
=
localtime
(
&rawtime
);
printf("Please
enter
year
month
day
hour
minute
second\n");
printf("For
example:
\n");
printf("2008
8
16
9
55
25\n");
scanf("%d
%d
%d
%d
%d
%d",
&year,
&month,
&mday,
&hh,&mm,&ss);
target_time->tm_year
=
year
-
1900;
target_time->tm_mon=
month
-
1;
target_time->tm_mday
=
mday
;
target_time->tm_hour
=
hh
;
target_time->tm_min
=
mm
;
target_time->tm_sec
=
ss
;
//
t
=
mktime
(target_time);
//
t
is
ready
to
use
printf("%s
",ctime(&t));
}
第3个回答 2012-03-18
这个试看看应该可以的
#include
<stdio.h>;
#include
<time.h>;
time_t
scanf_time(char
*
timestr)
{
struct
tm
t;
if(!timestr)
return
0;
memset(&t,
0,
sizeof(t));
sscanf(timestr,
"%02d%02d%02d
%02d:%02d:%02d",
&(t.tm_mday),
&(t.tm_mon),
&(t.tm_year),
&(t.tm_hour),
&(t.tm_min),
&(t.tm_sec));
t.tm_year
+=
100;
t.tm_mon
-=1;
return
mktime(&t);
}
int
main(int
argc,
char
*argv[])
{
time_t
x
=
0;
time_t
y
=
0;
x
=
scanf_time("160312
00:00:00");
y
=
time(NULL);
if(x>y){
//0=1970-00-01
08:00:00
struct
tm
t;
memcpy(&t,
localtime(&x),
sizeof(struct
tm));
printf("\n%d年-%d月-%d日\t%d时:%d分:%d秒\n",
t.tm_year
+
1900,
t.tm_mon+1,
t.tm_mday,
t.tm_hour,
t.tm_min,
t.tm_sec);
memcpy(&t,
localtime(&y),
sizeof(struct
tm));
printf("\n%d年-%d月-%d日\t%d时:%d分:%d秒\n",
t.tm_year
+
1900,
t.tm_mon+1,
t.tm_mday,
t.tm_hour,
t.tm_min,
t.tm_sec);
time_t
result
=
x
-
y;
memcpy(&t,
localtime(&result),
sizeof(struct
tm));
printf("\n%d年-%d月-%d日\t%d时:%d分:%d秒\n",
t.tm_year
-70,
t.tm_mon,
t.tm_mday-1,
t.tm_hour-8,
t.tm_min,
t.tm_sec);
}
system("PAUSE");
return
0;
}
第4个回答 2013-12-24
早就不用TC了,不清楚。
不知道你想要干什么,如果只是想简单地用字符串格式来输出现在的时间,用下面的代码就可以了。如果要自定格式,才需要用到struct
tm结构,不过不是你写的样子。
#include
<time.h>
#include
<stdio.h>
int
main()
{
time_t
lt;
lt
=
time(NULL);
printf("The
Calendar
Time
now
is
%s\n",
ctime(&
lt));
return
0;
}