在C语言中,可以使用time.h头文件中的函数来获取当前系统时间。具体步骤如下:
#include <time.h>
time_t now;
struct tm *local_time;
time(&now);
local_time = localtime(&now);
int year = local_time->tm_year + 1900;
int month = local_time->tm_mon + 1;
int day = local_time->tm_mday;
int hour = local_time->tm_hour;
int minute = local_time->tm_min;
int second = local_time->tm_sec;
完整的代码如下所示:
#include <stdio.h>
#include <time.h>
int main() {
time_t now;
struct tm *local_time;
time(&now);
local_time = localtime(&now);
int year = local_time->tm_year + 1900;
int month = local_time->tm_mon + 1;
int day = local_time->tm_mday;
int hour = local_time->tm_hour;
int minute = local_time->tm_min;
int second = local_time->tm_sec;
printf("当前时间:%d年%d月%d日 %d:%d:%d\n", year, month, day, hour, minute, second);
return 0;
}
运行该程序,即可获取当前系统时间并打印出来。