what is the effect of static

Discussion in 'C' started by anchitjindal07, Jul 13, 2009.

  1. anchitjindal07

    anchitjindal07 New Member

    Joined:
    Jul 13, 2009
    Messages:
    22
    Likes Received:
    0
    Trophy Points:
    1
    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;
    printf("%d\n",sub);
    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
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    You have undefined behaviour because sub[5] doesn't exist.
    Limit i to the range 0..4 and you should get consistent behaviour.
     
  3. anchitjindal07

    anchitjindal07 New Member

    Joined:
    Jul 13, 2009
    Messages:
    22
    Likes Received:
    0
    Trophy Points:
    1
    Thanks.....friend
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice