C語言實(shí)現(xiàn)簡(jiǎn)單的定時(shí)器
更新時(shí)間:2020年10月29日 12:54:59 作者:研究猿小劉
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)簡(jiǎn)單的定時(shí)器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了C語言實(shí)現(xiàn)簡(jiǎn)單的定時(shí)器的具體代碼,供大家參考,具體內(nèi)容如下
1.代碼分析
2.代碼
#include <stdio.h> #include <time.h> #include <conio.h> #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. 代碼抽象出一個(gè)定時(shí)器函數(shù) 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)) { //時(shí)間沒有到,啥也不做,空循環(huán) }else { //時(shí)間到了退出循環(huán) // printf("%s","hello"); break; } } }
完整代碼
#include <stdio.h> #include <time.h> #include <conio.h> #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)) { //時(shí)間沒有到,啥也不做,空循環(huán) }else { //時(shí)間到了退出循環(huán) // printf("%s","hello"); break; } } } int main( void ) { for(int i=0;i<10;i++){ timer(1); printf("%d\n",i); } getch(); }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C語言中g(shù)etch()函數(shù)詳解及簡(jiǎn)單實(shí)例
這篇文章主要介紹了C語言中g(shù)etch()函數(shù)詳解及簡(jiǎn)單實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-03-03C++實(shí)現(xiàn)教職工信息管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)教職工信息管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03