以下是错误程序源代码举例(其实是我自己在学习过程中出现的错误):
#include <stdio.h>
struct Date
{
int year;
int month;
};
struct Student
{
char sno[11];
char name[10];
int Gshu;
int Ccheng;
int Yyu;
float Average;
Date birthday;
};
int main()
{
struct Students stu;
printf("输入学号:");
gets(stu.sno);
printf("输入姓名:");
gets(stu.name);
printf("高数成绩:");
scanf("%d",&stu.Gshu);
printf("C程成绩:");
scanf("%d",&stu.Ccheng);
printf("英语成绩:");
scanf("%d",&stu.Yyu);
stu.Average=(stu.Gshu+stu.Ccheng+stu.Yyu)/3.0;
printf("输入出生年月");
scanf("%d%d",&stu.birthday.year,&stu.birthday.month);
printf("学号 姓名 高数 C程 英语 平均成绩 出生年月\n");
printf(" %11s",stu.sno);
printf(" %6s",stu.name);
printf(" %3d %3d %3d %4.1f",stu.Gshu,stu.Ccheng,stu.Yyu);
printf(" %d年%d月\n",stu.birthday.year,stu.birthday.month);
return 0;
}
运行错误如下:
这个地方的错误是将上文中的Student误写为Students,于是出现了未定义的错误,改正方式只要将Students改为Student就可以解决。但是如果不是因为笔误,而是正确的写出了Student,但是仍然为错误的话,解决方法是检查上文中有没有对Student进行定义(struct),通过对Student定义即在前文(主函数外)添加:struct Student,就可以解决。