|
Post by huangno1 on Mar 2, 2017 20:40:56 GMT -8
程式說明
練習傳遞參數進函式
#include <stdio.h>
#include <string.h>
// 定義結構資料型態 Student
// 每位 Student 含有姓名、性別、年齡和三個分數
struct Student {
char name [ 10 ] = { } ;
char gender = ' ' ;
int age = 0 ;
int scores [ 3 ] = { } ;
} ;
void Print(
Student s
)
{
printf("%s %c %d ",
s.name , s.gender , s.age
);
for (int i=0; i<3; i+=1) { printf("%d ",
s.scores [ i ]
); }
putchar('\n');
}
int main()
{
Student allen;
// 由使用者依序輸入姓名、性別、年齡和三個分數
scanf ( "%s %c %d" , allen.name , &allen.gender , &allen.age ) ;
for ( int i = 0 ; i < 3 ; i += 1 ) { scanf ( "%d" , &allen.scores [ i ] ) ; }
// 呼叫 Prin() 函式以輸出 allen
Print(
allen
);
return 0;
}
|
|