about the place of defining variable in c program!
一直以来的学习中关于变量的定义都是在使用之前,也就是说使用之前定义了都没问题,但是最近练习中却发现事实并非如此。
such as the example following:
#include <stdio.h>
struct date
{
int month;
int day;
int year;
}date1;
struct date date2;
struct student
{
char num[11];
char name[11];
char sex;
struct date birthday;
}qjl[3]={{"20041102215","qujl1",'M',8,23,1985},
{"20041102215","qujl2",'F',8,23,1985},
{"20041102215","qujl3",'M',8,23,1985}
};
main()
{
struct date date4;
struct student *p; //●
date4.month=6;
date4.day=10;
date4.year=3005;
/*date4={6,10,3006};*/ /*错误赋值方法*/
printf("%d-%d-%d/n",date4.year,date4.month,date4.day);
printf("NO. NAME SEX /n");
for(p=qjl;p<qjl+3;p++)
printf("%-19s %-11s%-2c/n",p->num,p->name,p->sex);
getch();
}
in the example ,when i define struct student *p; in the head of the program,it's true ,but when i define it between the sentence printf("NO. NAME SEX/n");and the for cyclic ,it will be false.
I don't know the reason of this instance,mybe i will know it later.
if you pass by my blog ,and you know the reason,please tell me!Thank you forver!