what is the effect of static

anchitjindal07's Avatar, Join Date: Jul 2009
Go4Expert Member
Hello friends Please help me with following problem in C

void main()
{
static int sub[5]={10,20,30,40,50};
int i=5;
clrscr();
sub[i]=i*i;
printf("%d\n",sub[i]);
getch();
}


Here the storage class of array sub[] is declared to be static and printf prints character corresponding to ASCII value 25

If we declare storage class of sub[] to be auto then printf() prints 25

Why this difference comes
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
You have undefined behaviour because sub[5] doesn't exist.
Limit i to the range 0..4 and you should get consistent behaviour.
0
anchitjindal07's Avatar, Join Date: Jul 2009
Go4Expert Member
Quote:
Originally Posted by xpi0t0s View Post
You have undefined behaviour because sub[5] doesn't exist.
Limit i to the range 0..4 and you should get consistent behaviour.
Thanks.....friend