C语言简单时钟程序实现
以下是一个简单的C语言时钟程序示例:
#include <stdio.h>
#include <time.h>
int main()
{
time_t now;
struct tm *tm_now;
while (1) {
now = time(NULL);
tm_now = localtime(&now);
printf('%02d:%02d:%02d\n', tm_now->tm_hour, tm_now->tm_min, tm_now->tm_sec);
sleep(1); // 等待1秒
}
return 0;
}
这个程序使用了time.h库中的time()和localtime()函数来获取当前时间,并使用printf()函数来输出时间。程序会一直循环输出时间,直到程序被终止。程序中的sleep()函数用来等待1秒钟,以便程序不会一直输出时间,导致CPU占用率过高。
原文地址: https://www.cveoy.top/t/topic/jwrc 著作权归作者所有。请勿转载和采集!