c语言输入学号姓名,输出学号姓名
时间: 2023-10-22 10:08:08 浏览: 683
好的,以下是一个简单的 C 代码示例,可以实现输入学号和姓名,并输出它们:
```c
#include <stdio.h>
int main() {
int student_id;
char student_name[50];
printf("请输入学号:");
scanf("%d", &student_id);
printf("请输入姓名:");
scanf("%s", student_name);
printf("学号:%d\n姓名:%s\n", student_id, student_name);
return 0;
}
```
运行程序后,在终端中输入学号和姓名,程序会输出它们。请注意,这个代码示例中没有进行任何输入错误的处理,这在实际应用中是需要注意的。
相关问题
c语言输入学号输出信息
假设学生信息包括学号、姓名和成绩,可以使用结构体来表示学生信息。下面是一个简单的示例代码:
```c
#include <stdio.h>
#define MAX_STUDENT_NUM 100
struct Student {
char id[20];
char name[20];
float score;
};
int main() {
struct Student students[MAX_STUDENT_NUM];
int studentNum = 0;
// 输入学生信息
printf("请输入学生信息(学号 姓名 成绩),以空格分隔,每个学生一行,输入 end 结束\n");
while (1) {
scanf("%s", students[studentNum].id);
if (strcmp(students[studentNum].id, "end") == 0) {
break;
}
scanf("%s%f", students[studentNum].name, &students[studentNum].score);
studentNum++;
if (studentNum >= MAX_STUDENT_NUM) {
printf("超过最大学生数!\n");
break;
}
}
// 输入学号查询学生信息
char searchId[20];
printf("请输入要查询的学生学号:");
scanf("%s", searchId);
int found = 0;
for (int i = 0; i < studentNum; i++) {
if (strcmp(searchId, students[i].id) == 0) {
printf("学号:%s,姓名:%s,成绩:%f\n", students[i].id, students[i].name, students[i].score);
found = 1;
break;
}
}
if (!found) {
printf("未找到该学生!\n");
}
return 0;
}
```
在程序中,首先定义了一个结构体 `Student`,包含了学号、姓名和成绩三个成员变量。然后定义了一个数组 `students` 来存储学生信息,最多可以存储 `MAX_STUDENT_NUM` 个学生。接下来使用 `while` 循环来不断输入学生信息,当输入 `end` 时退出循环。输入完成后,程序会要求输入一个学号,然后遍历数组 `students` 查找对应的学生信息,并输出到控制台上。如果未找到该学生,则输出提示信息。
c语言 输入姓名年龄学号密码成绩
以下是一个简单的 C 语言程序,可以让用户输入姓名、年龄、学号、密码和成绩:
```
#include <stdio.h>
int main() {
char name[20], student_id[20], password[20];
int age;
float score;
printf("请输入您的姓名:");
scanf("%s", name);
printf("请输入您的年龄:");
scanf("%d", &age);
printf("请输入您的学号:");
scanf("%s", student_id);
printf("请输入您的密码:");
scanf("%s", password);
printf("请输入您的成绩:");
scanf("%f", &score);
printf("您的姓名是:%s\n", name);
printf("您的年龄是:%d\n", age);
printf("您的学号是:%s\n", student_id);
printf("您的密码是:%s\n", password);
printf("您的成绩是:%f\n", score);
return 0;
}
```
注意,这个程序只是一个简单的示例,实际使用中可能需要更复杂的逻辑来处理输入的数据,比如对密码进行加密等。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)