11

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 the same name a?

EDIT: If not, why?

Conceptually, the local variable is stored on stack and functions are stored in text section of memory. So there shouldn't be a problem right?

When I tried this, compiler gave error. I am wondering if this is compiler specific or it is actually an error.

4
  • The error is likely elsewhere.
    – devnull
    Jan 24, 2014 at 6:20
  • can you specify the error Jan 24, 2014 at 6:22
  • a cannot be used as a function, or something like that
    – Dabo
    Jan 24, 2014 at 6:23
  • @Dabo, yes that's the error
    – SoulRayder
    Jan 24, 2014 at 6:29

4 Answers 4

11

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.

6

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

7
  • This is the right answer. There are function pointers, which are variables. But also, all function names are treated as const function pointers! So when you try to define your variable, there already exists a variable with the same name. Jan 24, 2014 at 6:28
  • So where are these pointer variables stored? If it is text memory, it still shouldn't be an issue right?
    – SoulRayder
    Jan 24, 2014 at 6:41
  • Saying that "'example' is a pointer to that function." is incorrect. In this case 'example' will give you the function address, but there is no pointer variable.
    – user694733
    Jan 24, 2014 at 7:34
  • 1
    @NicolasMiari This is not the right answer, because the OP explicitely speaks about a local variable, not a static global. A local variable will mask the global function name just like it would any symbol higher in the scope chain.
    – kuroi neko
    Jan 24, 2014 at 10:52
  • @user694733 small technicality, almost pedantic I would say. The symbol (function name) is treated as though there a const function pointer with the same name and signature. If you take your argument to the letter, there exist no 'variables' at all; only memory addresses. Treating some of them as variables is a high level, human-friendly (but artificial) construct. Jan 25, 2014 at 5:14
5

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.)

4
  • If he is actually getting a compiler error, that means that a difference in scope is not helping avoid the collision. Perhaps his variable is file-global, hence at the same scope as all the function names. Jan 24, 2014 at 6:30
  • @NicolasMiari, local variable with same name will also give a compiler error if he tries to actually call the function: error: called object ‘abc’ is not a function .
    – Dipto
    Jan 24, 2014 at 6:32
  • OK, so both will yield an error. I guess a local variable inside e.g. a for loop is no good either, because the scope of function names is 'pervasive'? (correct me if I'm wrong) Jan 24, 2014 at 6:34
  • You can declare a symbol a in your routine and then another symbol with name a in another scope inside that routine, e.g. in the code block of a for loop, and so on. Only the symbol with the closest scope will be recognised. If you only have a local variable a in a loop scope, you can call the function a() outside that scope, but not when the local variable a is visible. (GCC can warn you about shadowing with -Wshadow, but I'm surprised that this is not included in -Wall or even -Wextra.)
    – M Oehm
    Jan 24, 2014 at 6:57
4

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.