c语言1.5秒定时程序,C语言实现简单的定时器

本文实例为大家分享了C语言实现简单的定时器的具体代码,供大家参考,具体内容如下

1.代码分析

57efd0515f4c198abef050dc6efac762.png

2.代码

#include

#include

#include

#ifndef CLOCKS_PER_SEC

#define CLOCKS_PER_SEC 1000

#endif

int 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();

}

3. 代码抽象出一个定时器函数 void timer(long time)

void timer(long time){

clock_t start;

long count = 1;

start = clock();

while(1)

{

if((clock() - start) != (time*CLOCKS_PER_SEC))

{

//时间没有到,啥也不做,空循环

}else {

//时间到了退出循环

// printf("%s","hello");

break;

}

}

}

完整代码

#include

#include

#include

#ifndef CLOCKS_PER_SEC

#define CLOCKS_PER_SEC 1000

#endif

/**

* time 的单位为s

*/

void timer(long time){

clock_t start;

long count = 1;

start = clock();

while(1)

{

if((clock() - start) != (time*CLOCKS_PER_SEC))

{

//时间没有到,啥也不做,空循环

}else {

//时间到了退出循环

// printf("%s","hello");

break;

}

}

}

int main( void )

{

for(int i=0;i<10;i++){

timer(1);

printf("%d\n",i);

}

getch();

}

18e76b5799fbb505a15e8f502e2c8940.png

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部