Is it possible to declare multiple static variable with same name in a single C file?

Posted by Mohammed Khalid Kherani on Stack Overflow See other posts from Stack Overflow or by Mohammed Khalid Kherani
Published on 2010-05-28T12:33:19Z Indexed on 2010/05/28 12:41 UTC
Read the original article Hit count: 266

Filed under:
|

Hi Experts,

Is it possible to declare multiple static variables of same name in a single C file with different scopes? I wrote a simple programme to check this and in gcc it got compiled and worked fine. code:

static int sVar = 44;

void myPrint2()
{
    printf("sVar = %d\n", sVar++);
}


void myPrint()
{
    static int sVar =88;
    printf("sVar = %d\n", sVar++);
}

int main(void)

{

    static int sVar = 55;
    int i = 0;
    for (i = 0; i < 5; i++)
        myPrint();      
    printf("sVar = %d\n", sVar);
    myPrint2();
    return(0);
}

Now my question is since all "static" variable will reside in same section (.data) then how we can have multiple variable with same name in one section? I used objdump to check the different section and found that all Static variables (sVar) were in .data section but with different names

0804960c l O .data 00000004 sVar

08049610 l O .data 00000004 sVar.1785

08049614 l O .data 00000004 sVar.1792

Why compiler is changing the name of variables (since C doesnt support name mangling)?

Thanks in advance.

© Stack Overflow or respective owner

Related posts about c

    Related posts about static