在实际开发过程中,很多事情需要用到定时器。C语言也可以实现定时器的功能,例如下面代码:
#include <stdio.h>
#include <time.h>
#include <conio.h>
#ifndef CLOCKS_PER_SEC
#define CLOCKS_PER_SEC 1000
#endif
void main( void )
{
clock_t start;
long count = 1;
start = clock();
while(1)
{
if((clock() - start) == CLOCKS_PER_SEC)
{
printf("%ld\n",count++);
start = clock();
//break;
}
}
getch();
}
以上代码就是每秒钟打印一个数字,从1开始到∞,截图:
这是一个很简单的程序,只是给大家提供一种思路,不足之处,请大家多多指正!