温馨提示×

c语言怎么显示当前时间

小亿
106
2024-03-28 10:33:07
栏目: 编程语言

在C语言中,可以使用<time.h>头文件中的函数来显示当前时间。以下是一个简单的示例代码:

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

int main() {
    time_t current_time;
    struct tm *local_time;

    // 获取当前时间
    current_time = time(NULL);
    local_time = localtime(&current_time);

    // 显示当前时间
    printf("Current time: %s", asctime(local_time));

    return 0;
}

这段代码中,首先获取当前时间并将其存储在current_time变量中。然后使用localtime函数将当前时间转换为本地时间,并将结果存储在local_time结构体中。最后使用asctime函数将本地时间转换为可读的字符串,并通过printf函数输出到控制台。

0