Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   How are static variables stored in memory!!! (http://www.go4expert.com/forums/static-variables-stored-memory-t21668/)

micsom 5Apr2010 15:58

How are static variables stored in memory!!!
 
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;
}


xpi0t0s 6Apr2010 12:58

Re: How are static variables stored in memory!!!
 
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.


All times are GMT +5.5. The time now is 07:55.