以下是一个简单的C语言新年倒计时程序:
#include <stdio.h>
#include <time.h>
int main() {
int days, hours, minutes, seconds;
time_t t;
// 获取当前时间
time(&t);
struct tm *now = localtime(&t);
// 计算距离新年的天数、小时数、分钟数和秒数
struct tm newyear = *localtime(&t + (31 * 86400)); // 31天后是新的一年
newyear.tm_mday = 1; // 设定为新年的第一天
newyear.tm_isdst = -1; // 忽略夏令时
time_t newyear_t = mktime(&newyear); // 转换成时间戳
if (newyear_t == -1) {
printf("Error: unable to create new year time\n");
return 1;
}
double diff = difftime(newyear_t, t) / (24 * 3600); // 计算天数差
days = diff;
hours = diff * 24 - days * 24;
minutes = hours * 60 - (days * 24 * 60 + hours * 60);
seconds = minutes * 60 - (days * 24 * 60 + hours * 60 + minutes);
// 输出倒计时
printf("距离新年还有:%d 天 %d 小时 %d 分钟 %d 秒\n", days, hours, minutes, seconds);
return 0;
}
这个程序首先获取当前时间,然后计算距离新年的天数、小时数、分钟数和秒数,最后输出倒计时。注意,这个程序没有考虑闰年,如果需要考虑闰年,需要修改代码。