C语言结构体通过 scanf初始化

时间:2023-01-13 07:16:47


# include <stdio.h>
struct AGE
{
int year;
int month;
int day;
};
struct STUDENT
{
char name[20];
int num;
struct AGE birthday;
float score;
}; //分号不能省
int main(void)
{
struct STUDENT student1; /*用struct STUDENT结构体类型定义结构体变量student1*/
printf("请输入姓名:");
scanf("%s", student1.name); //不能写成&student1
printf("请输入学号:");
scanf("%d", &student1.num);
printf("请输入生日:");
scanf("%d", &student1.birthday.year);
scanf("%d", &student1.birthday.month);
scanf("%d", &student1.birthday.day);
printf("请输入成绩:");
scanf("%f", &student1.score);
printf("name: %s\n", student1.name); //不能写成&student1
printf("num: %d\n", student1.num);
printf("birthday: %d-%d-%d\n", student1.birthday.year, student1.birthday.month, student1.birthday.day);
printf("score: %.1f\n", student1.score);
return 0;
}
请输入姓名:lg
请输入学号:08300734
请输入生日:1988
05
03
请输入成绩:99
name: lg
num: 8300734
birthday: 1988-5-3
score: 99.0