写一段C语言代码,将字符串"20201112031836" 直接转换成整型的年、月、日、时、分、秒,保存到结构体time中
时间: 2023-10-20 21:16:02 浏览: 88
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Time {
int year;
int month;
int day;
int hour;
int minute;
int second;
};
int main() {
char strTime[] = "20201112031836";
struct Time time;
memset(&time, 0, sizeof(struct Time)); // 初始化结构体
// 将字符串中的年、月、日、时、分、秒分别赋值给结构体成员
sscanf(strTime, "%4d%2d%2d%2d%2d%2d",
&time.year, &time.month, &time.day,
&time.hour, &time.minute, &time.second);
printf("Year: %d\n", time.year);
printf("Month: %d\n", time.month);
printf("Day: %d\n", time.day);
printf("Hour: %d\n", time.hour);
printf("Minute: %d\n", time.minute);
printf("Second: %d\n", time.second);
return 0;
}
```
阅读全文