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
相关推荐
喵了meme1 小时前
C语言实战4
c语言·开发语言
智者知已应修善业2 小时前
【求中位数】2024-1-23
c语言·c++·经验分享·笔记·算法
程序员zgh5 小时前
Linux系统常用命令集合
linux·运维·服务器·c语言·开发语言·c++
Bigan(安)6 小时前
【奶茶Beta专项】【LVGL9.4源码分析】09-core-obj_class对象类系统
linux·c语言·mcu·arm·unix
程序员zgh7 小时前
常用通信协议介绍(CAN、RS232、RS485、IIC、SPI、TCP/IP)
c语言·网络·c++
Bigan(安)7 小时前
【奶茶Beta专项】【LVGL9.4源码分析】08-theme主题管理
linux·c语言·mcu·arm·unix
了一梨7 小时前
外设与接口:按键输入 (libgpiod)
linux·c语言
昔时扬尘处8 小时前
【Files Content Replace】文件夹文件内容批量替换自动化测试脚本
c语言·python·pytest·adi
芯联智造9 小时前
【stm32简单外设篇】- 28BYJ-48 步进电机(配 ULN2003 驱动板)
c语言·stm32·单片机·嵌入式硬件
橘子真甜~9 小时前
C/C++ Linux网络编程13 - 传输层TCP协议详解(面向字节流和有连接)
linux·运维·服务器·c语言·网络·c++·tcp/ip