C語言實(shí)現(xiàn)簡單的定時(shí)器
本文實(shí)例為大家分享了C語言實(shí)現(xià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();
}

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C語言實(shí)現(xiàn)簡單計(jì)算器功能(1)
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)簡單計(jì)算器功能的第一部分,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-02-02
Qt數(shù)據(jù)庫應(yīng)用之實(shí)現(xiàn)圖片轉(zhuǎn)pdf
這篇文章主要為大家詳細(xì)介紹了如何利用Qt實(shí)現(xiàn)圖片轉(zhuǎn)pdf功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定參考價(jià)值,需要的可以了解一下2022-06-06
C++類與對(duì)象深入之引用與內(nèi)聯(lián)函數(shù)與auto關(guān)鍵字及for循環(huán)詳解
朋友們好,這篇播客我們繼續(xù)C++的初階學(xué)習(xí),現(xiàn)在對(duì)一些C++的入門知識(shí)做了些總結(jié),整理出來一篇博客供我們一起復(fù)習(xí)和學(xué)習(xí),如果文章中有理解不當(dāng)?shù)牡胤?還希望朋友們在評(píng)論區(qū)指出,我們相互學(xué)習(xí),共同進(jìn)步2022-06-06
c語言枚舉類型enum的用法及應(yīng)用實(shí)例
enum是C語言中的一個(gè)關(guān)鍵字,enum叫枚舉數(shù)據(jù)類型,枚舉數(shù)據(jù)類型描述的是一組整型值的集合,這篇文章主要給大家介紹了關(guān)于c語言枚舉類型enum用法及應(yīng)用的相關(guān)資料,需要的朋友可以參考下2021-07-07
Qt實(shí)現(xiàn)編輯數(shù)據(jù)庫數(shù)據(jù)的方法詳解
這篇文章主要為大家詳細(xì)介紹了Qt是如何實(shí)現(xiàn)編輯數(shù)據(jù)庫數(shù)據(jù)的,文中的示例代碼簡潔易懂,對(duì)我們深入了解QT有一定的幫助,感興趣的小伙伴可以了解一下2023-02-02

