C 练习实例71-结构体

**题目:**编写input()和output()函数输入,输出5个学生的数据记录。

代码:

cpp 复制代码
#include <stdio.h>
typedef struct{
	char name[20];
	char sex[2];
	int age;
}student;	//定义了一种新的数据类型,叫做:student
void input(student *stu)
{
	printf("请输入5个学生的信息:姓名 性别 年龄:\n");
	for(int i=0;i<5;i++){
		scanf("%s %s %d",stu[i].name,stu[i].sex,&stu[i].age);
	}
}
void output(student *stu)
{
	 printf("5个学生的信息如下:\n姓名  性别  年龄\n");
	for(int i=0;i<5;i++){
		printf("%s  %s    %d\n",stu[i].name,stu[i].sex,stu[i].age);
	}
}
int main()
{
	student stu[5];	//结构体数组
	input(stu);
	output(stu);
	return 0;
}

输入:

bash 复制代码
请输入5个学生的信息:姓名 性别 年龄:
刘一 男 10
刘二 女 11
刘三 男 12
刘四 女 13
刘五 男 14

输出:

bash 复制代码
5个学生的信息如下:
姓名  性别  年龄
刘一  男    10
刘二  女    11
刘三  男    12
刘四  女    13
刘五  男    14
相关推荐
无限进步_1 小时前
C语言宏的魔法:探索offsetof与位交换的奇妙世界
c语言·开发语言·windows·后端·算法·visual studio
代码雕刻家1 小时前
C语言关于换行符的注意事项
c语言·开发语言
咕咕嘎嘎10241 小时前
C/C++内存对齐
java·c语言·c++
缘三水1 小时前
【C语言】14.指针(4)
c语言·开发语言·指针·语法
Felven2 小时前
C. Isamatdin and His Magic Wand!
c语言·数据结构·算法
吗~喽2 小时前
【C++】模板进阶
c语言·开发语言·c++
京井3 小时前
从中序与后序遍历序列构造二叉树解题思路
c语言·算法
Tandy12356_3 小时前
手写TCP/IP协议栈——ARP超时重新请求
c语言·c++·网络协议·计算机网络
代码雕刻家4 小时前
C语言中fgets函数详解
c语言·开发语言
Bigan(安)4 小时前
【奶茶Beta专项】【LVGL9.4源码分析】04-OS抽象层
linux·c语言·mcu·arm·unix