我目前正在从事 ac 项目,我正在尝试从系统中获取日期并将其构建为字符串。
我对 c 很陌生,所以我目前正在学习,但我无法让它发挥作用。下面是我的代码
time_t now = time(NULL);
struct tm *t = localtime(&now);
char currentDate[13];
char day[3];
char month[3];
char year[5];
char hour[3];
char min[3];
sprintf(day, "%02d", t->tm_mday, sizeof(day));
sprintf(month, "%02d", t->tm_mon+1, sizeof(month));
sprintf(year, "%04d", t->tm_year + 1900, sizeof(year));
sprintf(hour, "%02d", t->tm_hour, sizeof(hour));
sprintf(min, "%02d", t->tm_min, sizeof(min));
strcat(currentDate, day);
strcat(currentDate, month);
strcat(currentDate, year);
strcat(currentDate, hour);
strcat(currentDate, min);
printf("Current Date: %s", currentDate);
当它执行 printf 时,它只会打印一堆垃圾,例如
当前日期:ÃÃÃ××Z¤ÿ·øÃéôÃÿ·^XÃé<95>úþ·^F^FÃ^P^_
我究竟做错了什么。