Can local variables and functions have the same names in C?

前端 未结 4 1190
予麋鹿
予麋鹿 2021-01-04 10:30

Suppose I have a single .c file in which I have a local variable a. Can I also have a function in that c file which has t

4条回答
  •  情歌与酒
    2021-01-04 10:59

    Just for completion's sake, there is actually a way to refer to global identifiers, even if they are shadowed in C.

    In kuroi neko's snippet, that would look like this:

    void a (void)
    {
        // whatever
    }
    
    {
        int a;
    
        a++; // no problem, boss
        {
             extern void a(void);
             a(); // no problem either
        }
    }
    

    I wouldn't do that though. Use proper names instead.

提交回复
热议问题