请用标准数据类型创建结构体,用于描述一个人的信息,结构体包括三个成员,分别为姓名、性别、3门课的成绩。 输入要求 输入为三行。第一行输入人的姓名(可能包含空格);第二行为一个字母,代表性别;第三行为3个整数,表示3门课的成绩。假设输入的数据都是在有效范围内的。
时间: 2024-01-22 21:17:19 浏览: 154
以下是使用标准数据类型创建的描述人信息的结构体示例代码,包括姓名、性别和3门课成绩的成员:
```c
#include <stdio.h>
struct Person {
char name[20];
char gender;
int score[3];
};
int main() {
struct Person p;
printf("请输入姓名:\n");
scanf("%[^\n]", p.name); // %[^\n] 表示读取到换行符之前的所有字符
getchar(); // 读取并丢弃换行符
printf("请输入性别:\n");
scanf("%c", &p.gender);
printf("请输入3门课的成绩:\n");
scanf("%d %d %d", &p.score[0], &p.score[1], &p.score[2]);
printf("姓名:%s\n", p.name);
printf("性别:%c\n", p.gender);
printf("成绩:%d %d %d\n", p.score[0], p.score[1], p.score[2]);
return 0;
}
```
在上面的示例中,我们首先定义了一个名为 `Person` 的结构体,并在其中定义了三个成员,分别是 `name`(姓名,类型为字符串)、`gender`(性别,类型为字符)和 `score`(成绩,类型为整型数组)。然后在 `main` 函数中,我们创建了一个 `Person` 类型的结构体变量 `p`,并通过 `scanf` 函数从标准输入中读取用户输入的姓名、性别和3门课程的成绩。最后,我们将 `p` 变量的成员值打印到屏幕上,以验证输入和结构体定义的正确性。
阅读全文
相关推荐


















