How are static variables stored in memory!!!

Discussion in 'C' started by micsom, Apr 5, 2010.

  1. micsom

    micsom New Member

    Joined:
    Oct 13, 2008
    Messages:
    39
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    I*N*D*I*A*
    initialized static variables are stored in the Data segment, but in that case there will be two copies of static int i in the data segment according to the given code below, but how is it possible??..can some1 please tell me how are they stored in the memory, because this program is working fine..
    Code:
    #include <stdio.h>
    int fun1();
    int fun2();
    int main (int argc, const char * argv[]) {
        // insert code here...
        printf("Hello, World!\n");
        fun1();
        fun2();
        return 0;
    }
    int fun1(){
    	static int i=1;
    	printf("i=%d\n",i);
    	return 1;
    }
    int fun2(){
    	static int i=2;
    	printf("i=%d\n",i);
    	return 1;
    }
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    A variable name only represents a location in memory. Since each i is in a different function the compiler can tell the difference and there is no conflict. Variable storage only stores the value, not the name.
     

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