C語言實現簡單的定時器
更新時間:2020年10月29日 12:54:59 作者:研究猿小劉
這篇文章主要為大家詳細介紹了C語言實現簡單的定時器,文中示例代碼介紹的非常詳細c語言實現定時器,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了C語言實現簡單的定時器的具體代碼,供大家參考c語言實現定時器,具體內容如下
1.代碼分析
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(); }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。