Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   what is the effect of static (http://www.go4expert.com/forums/effect-static-t18527/)

anchitjindal07 13Jul2009 20:02

what is the effect of static
 
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

xpi0t0s 14Jul2009 00:57

Re: what is the effect of static
 
You have undefined behaviour because sub[5] doesn't exist.
Limit i to the range 0..4 and you should get consistent behaviour.

anchitjindal07 14Jul2009 06:06

Re: what is the effect of static
 
Quote:

Originally Posted by xpi0t0s (Post 52453)
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


All times are GMT +5.5. The time now is 00:50.