检索一个指定的学生信息并返回,由主函数打印在屏幕上c语言
时间: 2023-04-10 15:02:29 浏览: 44
好的,这是一个编程类的问题,我可以回答。以下是一个简单的示例代码:
```c
#include <stdio.h>
#include <string.h>
struct Student {
char name[20];
int age;
char gender[10];
char major[20];
};
int main() {
struct Student students[3] = {
{"Tom", 20, "Male", "Computer Science"},
{"Mary", 19, "Female", "Mathematics"},
{"John", 21, "Male", "Physics"}
};
char search_name[20];
printf("Enter the name of the student you want to search: ");
scanf("%s", search_name);
int i;
for (i = 0; i < 3; i++) {
if (strcmp(students[i].name, search_name) == 0) {
printf("Name: %s\n", students[i].name);
printf("Age: %d\n", students[i].age);
printf("Gender: %s\n", students[i].gender);
printf("Major: %s\n", students[i].major);
return 0;
}
}
printf("Sorry, the student with name %s is not found.\n", search_name);
return 0;
}
```
这个程序定义了一个结构体 `Student`,包含了学生的姓名、年龄、性别和专业。然后在主函数中创建了一个包含三个学生信息的数组。用户输入要搜索的学生姓名,程序遍历数组,找到匹配的学生信息并打印在屏幕上。如果没有找到,则输出提示信息。