运用C语言 7-7 按格式输出日期 分数 10 作者 李体新 单位 保定学院 本题目要求输入代表日期的3个整数Y(年)、M(月)和D(日),然后以指定格式输出日期。 输入格式: 输入代表日期的3个合法整数Y(年)、M(月)和D(日)。 输出格式: 以指定的三种格式输出日期。
时间: 2024-09-26 07:13:37 浏览: 55
在C语言中,为了按特定格式输出日期,你可以使用`<stdio.h>`库中的`printf()`函数配合字符串格式化控制符。这里提供一个简单的示例来满足要求:
```c
#include <stdio.h>
int main() {
int Y, M, D;
// 输入日期
printf("请输入年份(Y): ");
scanf("%d", &Y);
printf("请输入月份(M): ");
scanf("%d", &M);
printf("请输入日期(D): ");
scanf("%d", &D);
// 指定的输出格式:
// 格式1 (例如:YYYY-MM-DD)
printf("格式1: %04d-%02d-%02d\n", Y, M, D);
// 格式2 (例如:DD/MM/YYYY)
printf("格式2: %02d/%02d/%04d\n", D, M, Y);
// 格式3 (例如:MM/DD/YYYY)
printf("格式3: %02d/%02d/%04d\n", M, D, Y);
return 0;
}
```
在这个程序里,我们首先通过`scanf()`函数获取用户输入的年、月、日。然后,`%04d`, `%02d`等是格式化占位符,`04d`表示宽度为4的整数,并自动填充前导零,类似地,`%02d`表示两位数,不足的前面补0。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![c](https://img-home.csdnimg.cn/images/20250102104920.png)
![text/x-c](https://img-home.csdnimg.cn/images/20250102104920.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044947.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)