#include <sys/timeb.h>
#include <time.h>
int main(void) //判断经过的时间
{
unsigned long t1,t2,elapsed_time
unsigned long t11,t21
int s,ms
struct timeb timebuffer
ftime(&timebuffer)
t11=timebuffer.time
t1=timebuffer.millitm
//程序体
ftime(&timebuffer)
t21=timebuffer.time
t2=timebuffer.millitm
elapsed_time =1000*(t21-t11)+t2-t1
s=elapsed_time/1000
ms=elapsed_time-s*1000
printf("%ds:%dms",s,ms)
return 0
}
在c语言中,我们经常面临需要将字符串转换为整型,以及将整型转化为字符串的问题。将字符串转换为整数的函数在stdlib.h中定义:int atoi(const char *str)你可以把时间转化成毫秒,以字符串方式存储。减法操作时用上面的函数相减,然后再转化成字符串存储,至于时分秒的表示方式,自己慢慢换算吧