C Exercise 71
100 Classic C Language Examples
**Problem:** Write input() and output() functions to input and output the data records of 5 students.
**Analysis:** None.
Program source code:
// Created by www.runoob.com on 15/11/9.
// Copyright © 2015 Runoob Tutorial. All rights reserved.
//
#include
#include
typedef struct{
char name[20];
char sex[5];
int age;
}Stu;
void input(Stu*stu);
void output(Stu*stu);
int main()
{
Stu stu[5];
printf("请输入5个学生的信息:姓名 性别 年龄:\n");
input(stu);
printf("5个学生的信息如下:\n姓名 性别 年龄\n");
output(stu);
system("pause");
return 0;
}
void input(Stu*stu)
{
int i;
for(i=0;i<5;i++)
scanf("%s%s%d",stu[i].name,stu[i].sex,&(stu[i].age));
}
void output(Stu*stu)
{
int i;
for(i=0;i<5;i++)
printf("%s %s %d\n",stu[i].name,stu[i].sex,stu[i].age);
}
The output of the above program is:
Please enter information for 5 students: Name Gender Age:
aaa m 15
bbb m 16
ccc m 15
ddd m 17
eee m 16
The information for 5 students is as follows:
姓名 性别 年龄
aaa m 15
bbb m 16
ccc m 15
ddd m 17
eee m 16