#include"stdio.h"
struct student
{
char name[50];
int age;
float exam[5];
}st;
void main()
{
int i;
float ave;
float sum=0;
printf("输入学生名字:");
scanf("%s",st.name[50]);//字符串一整串输入输出用%s//
printf("请输入学生年龄:");
scanf("%d",&st.age);
for(i=0;i<5;i++)
{
printf("输入成绩:");
scanf("%f",&st.exam[i]);
sum+=st.exam[i];
}
ave=sum/5;
printf("该名同学的成绩平均分为%.2f\n",ave);
}
为什么输入字符串%s不加&,运行时输入人名后程序就结束了,是什么原因?加了&才能正常运行
scanf("%s",st.name);
& 是 取地址, 输入 需要用到变量的地址。