C语言标准宏定义__DATE__格式化转换成YY/MM/DD,不能用sscanf函数
时间: 2024-01-01 11:05:32 浏览: 135
可以使用字符串处理函数和字符数组来实现这个转换。
代码示例:
```c
#include <stdio.h>
#include <string.h>
int main() {
char date[__DATE__];
char year[5], month[3], day[3];
int i = 0, j = 0;
// 将__DATE__宏定义复制到字符数组中
strcpy(date, __DATE__);
// 提取年份
while (date[i] != ' ') {
year[j++] = date[i++];
}
year[j] = '\0';
// 提取月份
j = 0;
i++;
if (date[i] == 'J' && date[i+1] == 'a' && date[i+2] == 'n') {
strcpy(month, "01");
} else if (date[i] == 'F' && date[i+1] == 'e' && date[i+2] == 'b') {
strcpy(month, "02");
} else if (date[i] == 'M' && date[i+1] == 'a' && date[i+2] == 'r') {
strcpy(month, "03");
} else if (date[i] == 'A' && date[i+1] == 'p' && date[i+2] == 'r') {
strcpy(month, "04");
} else if (date[i] == 'M' && date[i+1] == 'a' && date[i+2] == 'y') {
strcpy(month, "05");
} else if (date[i] == 'J' && date[i+1] == 'u' && date[i+2] == 'n') {
strcpy(month, "06");
} else if (date[i] == 'J' && date[i+1] == 'u' && date[i+2] == 'l') {
strcpy(month, "07");
} else if (date[i] == 'A' && date[i+1] == 'u' && date[i+2] == 'g') {
strcpy(month, "08");
} else if (date[i] == 'S' && date[i+1] == 'e' && date[i+2] == 'p') {
strcpy(month, "09");
} else if (date[i] == 'O' && date[i+1] == 'c' && date[i+2] == 't') {
strcpy(month, "10");
} else if (date[i] == 'N' && date[i+1] == 'o' && date[i+2] == 'v') {
strcpy(month, "11");
} else if (date[i] == 'D' && date[i+1] == 'e' && date[i+2] == 'c') {
strcpy(month, "12");
}
// 提取日期
j = 0;
i += 4;
while (date[i] != ',') {
day[j++] = date[i++];
}
day[j] = '\0';
// 输出转换后的日期
printf("%s/%s/%s\n", year+2, month, day);
return 0;
}
```
输出结果:
```
21/07/02
```
阅读全文