以下是一个简单的C语言时钟程序,使用time.h库和Windows API函数:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>

int main()
{
    time_t current_time;
    struct tm * time_info;
    char time_string[9];
    int hour, minute, second;

    while (1) {
        // 获取当前时间
        time(&current_time);
        time_info = localtime(&current_time);

        // 格式化时间字符串
        strftime(time_string, sizeof(time_string), "%H:%M:%S", time_info);

        // 获取时分秒
        hour = time_info->tm_hour;
        minute = time_info->tm_min;
        second = time_info->tm_sec;

        // 打印时间
        printf("%s\r", time_string);
        fflush(stdout);

        // 暂停一秒
        Sleep(1000);
    }

    return 0;
}

该程序使用while循环不断获取当前时间,并使用strftime函数将时间格式化为HH:MM:SS的字符串。然后,它打印该字符串并暂停一秒钟,以创建一个类似于电子时钟的效果。


原文地址: https://www.cveoy.top/t/topic/bD9k 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录