我在C中经过一段时间后如何停止while循环,我在c ++中做到了,我尝试将其转换为c(下图),但是没有用
#include <time.h>
int main(void)
{
time_t endwait;
time_t start;
endwait = start + seconds ;
while (start < endwait)
{
/* Do stuff while waiting */
}
}
如何尝试我的测试代码:
#include <stdio.h>
#include <time.h>
#include <unistd.h>
int main(void)
{
time_t endwait;
time_t start = time(NULL);
time_t seconds = 10; // end loop after this time has elapsed
endwait = start + seconds;
printf("start time is : %s", ctime(&start));
while (start < endwait)
{
/* Do stuff while waiting */
sleep(1); // sleep 1s.
start = time(NULL);
printf("loop time is : %s", ctime(&start));
}
printf("end time is %s", ctime(&endwait));
return 0;
}
示例输出是:
wugq@SSDEV016:~/tools/test $ ./a.out
start time is : Fri Jan 17 17:12:57 2014
loop time is : Fri Jan 17 17:12:58 2014
loop time is : Fri Jan 17 17:12:59 2014
loop time is : Fri Jan 17 17:13:00 2014
loop time is : Fri Jan 17 17:13:01 2014
loop time is : Fri Jan 17 17:13:02 2014
loop time is : Fri Jan 17 17:13:03 2014
loop time is : Fri Jan 17 17:13:04 2014
loop time is : Fri Jan 17 17:13:05 2014
loop time is : Fri Jan 17 17:13:06 2014
loop time is : Fri Jan 17 17:13:07 2014
end time is Fri Jan 17 17:13:07 2014
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句