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

前端 未结 4 1162
予麋鹿
予麋鹿 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.

    0 讨论(0)
  • 2021-01-04 11:01

    You cant because if you have example(), 'example' is a pointer to that function.

    0 讨论(0)
  • 2021-01-04 11:07

    I assume you have something like that:

    void a (void)
    {
        // whatever
    }
    
    int main(void)
    {
        int a;
        a++; // no problem, boss
        a(); // <-- compiler tantrum: variable used as a function
        // whatever
    }
    

    The error you are getting is due to the fact that you are using a as a function.

    Each time you open a curly brace, you define a new local scope, where you are free to redefine symbols that exist in a higher scope.

    In that case, the identifier a inside the block is refering to a local variable, so you can't use it as a function.
    a at toplevel is a function, but inside the block it is shadowed by the local variable definition with the same name.
    It means you cannot call the function a from within that block (and any other embedded sub-blocks, for that matter). (more precisely, you cannot access the function a by its name, but you could still call it by having a pointer to that function accessable from within that scope)

    This should be an incentive to define meaningful names for your functions and other global symbols, since they will have to coexist in the toplevel scope and run the risk of being shadowed by any lower scope symbols.

    As other answers stated, there are mechanisms in other languages like C++ called "scope resolution modifiers" that allow to explicitely designate the scope of a symbol, but that does not exist in C.

    0 讨论(0)
  • 2021-01-04 11:16

    You can declare local variables with the same name as a global variable, but the local variable will shadow the global. As long as your local a is in scope, the symbol a refers to your local variable.

    Some languages allow to refer to the global symbols via special syntax, such as ::a or .a. C is not one of these languages.

    (As a side note: You shouldn't give global functions names that are likely to interfere with locals. Names of locals are usually short and don't carry much information - it is not needed, bacause the context is clear. Global variables have to share one big namespace with other variables. It is recommended to provide a bit of context in your name in order to avoid name clashes and shadowing.)

    0 讨论(0)
提交回复
热议问题