what is the effect of static

Go4Expert Member
13Jul2009,20:02   #1
anchitjindal07's Avatar
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
Mentor
14Jul2009,00:57   #2
xpi0t0s's Avatar
You have undefined behaviour because sub[5] doesn't exist.
Limit i to the range 0..4 and you should get consistent behaviour.
Go4Expert Member
14Jul2009,06:06   #3
anchitjindal07's Avatar
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