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
相关推荐
Discipline~Hai2 小时前
ARM01-ARM体系架构
linux·c语言·arm开发·架构
cjr_xyi4 小时前
AT1202Contest_c binarydigit 题解
c语言·c++·算法
Navigator_Z5 小时前
LeetCode //C - 1156. Swap For Longest Repeated Character Substring
c语言·算法·leetcode
无相求码5 小时前
const vs #define:C语言常量定义的差异
c语言·算法
Android洋芋6 小时前
AI辅助C盘清理
c语言·开发语言·人工智能·ai辅助c盘清理
ScilogyHunter6 小时前
GCC/Clang 原始字符串详解
c语言·原始字符串
麻瓜老宋7 小时前
AI开发C语言应用按步走,表达式计算器calc的第四步,交互式 REPL 模式
c语言·开发语言·atomcode
十月的皮皮8 小时前
stm20260719-STM32F103RCT6_HAL库_三人抢答器
c语言·stm32·单片机·嵌入式硬件·stm32cubemx·hal库
特立独行的猫a9 小时前
Python的C/C++三方库移植到鸿蒙PC实战踩坑记
c语言·c++·python·harmonyos·三方库移植·鸿蒙pc
wuyk5559 小时前
53.嵌入式开发中栈溢出的深度解析:从HardFault到解决方案
c语言·stm32·单片机