C 練習實例71
題目:編寫input()和output()函數輸入,輸出5個學生的數據記錄。
程式分析:無。
程式源代碼:
//  Created by www.xuhuhu.com on 15/11/9.
//  Copyright © 2015年 IT研修. All rights reserved.
//
#include<stdio.h>
#include<stdlib.h>
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);
}
以上程式執行輸出結果為:
請輸入5個學生的資訊:姓名 性別 年齡: aaa m 15 bbb m 16 ccc m 15 ddd m 17 eee m 16 5個學生的資訊如下: 姓名 性別 年齡 aaa m 15 bbb m 16 ccc m 15 ddd m 17 eee m 16

 C 語言經典100例