C语言输入3个学生的信息,放在结构体数组中,要求输出全部学生的信息
时间: 2023-07-14 08:13:56 浏览: 35
可以使用如下的C代码实现:
```c
#include <stdio.h>
struct student {
char name[20];
int age;
float score;
};
int main() {
struct student stu[3];
for (int i = 0; i < 3; i++) {
printf("请依次输入第%d个学生的姓名、年龄和成绩:\n", i+1);
scanf("%s %d %f", stu[i].name, &stu[i].age, &stu[i].score);
}
printf("全部学生的信息如下:\n");
for (int i = 0; i < 3; i++) {
printf("姓名:%s,年龄:%d,成绩:%.2f\n", stu[i].name, stu[i].age, stu[i].score);
}
return 0;
}
```
在上面的代码中,我们定义了一个`struct student`结构体来表示每个学生的信息,包括姓名`name`、年龄`age`和成绩`score`。然后我们定义了一个数组`stu`来存储三个学生的信息,通过循环输入每个学生的信息,并将其存储到数组中。最后,我们再次使用循环输出所有学生的信息。
相关问题
编写程序:有3个学生的信息,放在结构体数组中,要求输出全部学生的信息
### 回答1:
可以使用循环遍历结构体数组,输出每个学生的信息,例如:for (int i = 0; i < 3; i++) { printf("学生%d:%s %d\n", i+1, stu[i].name, stu[i].age); }
### 回答2:
要编写程序输出3个学生的信息,可以使用结构体数组来保存学生的信息。
首先,我们需要定义一个结构体来表示学生的信息。每个学生的信息包括姓名、年龄和学号。可以这样定义结构体:
```c
typedef struct {
char name[20];
int age;
int studentId;
} Student;
```
接下来,我们创建一个包含3个学生的结构体数组,并为每个学生的信息赋值。可以通过循环来完成这个操作:
```c
Student students[3];
for (int i = 0; i < 3; i++) {
printf("请输入第 %d 个学生的姓名:", i+1);
scanf("%s", students[i].name);
printf("请输入第 %d 个学生的年龄:", i+1);
scanf("%d", &students[i].age);
printf("请输入第 %d 个学生的学号:", i+1);
scanf("%d", &students[i].studentId);
}
```
最后,我们使用循环遍历结构体数组,输出每个学生的信息:
```c
for (int i = 0; i < 3; i++) {
printf("第 %d 个学生的姓名:%s\n", i+1, students[i].name);
printf("第 %d 个学生的年龄:%d\n", i+1, students[i].age);
printf("第 %d 个学生的学号:%d\n", i+1, students[i].studentId);
}
```
这样,程序就能输出全部学生的信息了。
### 回答3:
下面是一个用C语言编写的程序,用来存储3个学生的信息,并输出所有学生的信息:
#include <stdio.h>
// 定义学生结构体
struct Student {
char name[20];
int age;
float score;
};
int main() {
// 定义存储3个学生的结构体数组
struct Student students[3];
// 输入学生信息
for (int i = 0; i < 3; i++) {
printf("请输入第%d个学生的姓名:", i+1);
scanf("%s", students[i].name);
printf("请输入第%d个学生的年龄:", i+1);
scanf("%d", &students[i].age);
printf("请输入第%d个学生的分数:", i+1);
scanf("%f", &students[i].score);
}
// 输出学生信息
printf("所有学生的信息如下:\n");
for (int i = 0; i < 3; i++) {
printf("姓名:%s 年龄:%d 分数:%.2f\n", students[i].name, students[i].age, students[i].score);
}
return 0;
}
运行以上程序,会依次提示输入3个学生的姓名、年龄和分数,然后输出所有学生的信息。每个学生的信息包括姓名、年龄和分数。
用C语言 定义一个学生类型的结构体,包括学号 姓名和成绩 定义五个学生变量需要定义结构体数组 循环输入学生信息 循环输出学生信息
以下是用C语言定义一个学生类型的结构体,包括学号、姓名和成绩,定义五个学生变量并需要定义结构体数组,循环输入学生信息,循环输出学生信息的代码示例:
```c
#include <stdio.h>
#define MAX_STUDENTS 5
struct student {
int id;
char name[20];
double score;
};
int main() {
struct student students[MAX_STUDENTS];
// Input student information
for (int i = 0; i < MAX_STUDENTS; i++) {
printf("Enter student %d information:\n", i+1);
printf("ID: ");
scanf("%d", &students[i].id);
printf("Name: ");
scanf("%s", students[i].name);
printf("Score: ");
scanf("%lf", &students[i].score);
}
// Output student information
printf("Student Information:\n");
for (int i = 0; i < MAX_STUDENTS; i++) {
printf("ID: %d, Name: %s, Score: %.2lf\n", students[i].id, students[i].name, students[i].score);
}
return 0;
}
```
这个程序定义了一个名为`student`的结构体,包含了三个成员:`id`表示学号(整型),`name`表示姓名(字符数组),`score`表示成绩(浮点型)。然后定义了一个长度为5的结构体数组`students`用来存储五个学生的信息。
在输入学生信息时,通过一个循环来逐一输入每个学生的学号、姓名和成绩。在输出学生信息时,同样也通过一个循环来逐一输出每个学生的信息。
注意,在输入姓名时使用了`scanf("%s", students[i].name)`,这里没有使用`&`符号。这是因为字符数组名本身就是一个指针,不需要再取地址。
相关推荐















