C 語言實例 - 使用結構體(struct)
使用結構體(struct)存儲學生資訊。
實例
#include <stdio.h>
struct student
{
char name[50];
int roll;
float marks;
} s;
int main()
{
printf("輸入資訊:\n");
printf("名字: ");
scanf("%s", s.name);
printf("編號: ");
scanf("%d", &s.roll);
printf("成績: ");
scanf("%f", &s.marks);
printf("顯示資訊:\n");
printf("名字: ");
puts(s.name);
printf("編號: %d\n",s.roll);
printf("成績: %.1f\n", s.marks);
return 0;
}
輸出結果為:
輸入資訊: 名字: zaixian 編號: 123 成績: 89 顯示資訊: 名字: zaixian 編號: 123 成績: 89.0