1

Example code is as follows, both 'a' are file scope:

1 ...
2 int a;
3 int a;
4 ...   // which 'a' is visible?

I know that the two declarations for 'a' are for the same object. But every identifier has a scope, the scopes of the two 'a' should overlap at line 4, which one is visible? If the second 'a' is visible only, does that mean this situation is like the following:

{
    int a;
    {
           int a; // the scope of the first 'a' is hidden
    }
}

Thanks

password636
  • 981
  • 1
  • 7
  • 18

2 Answers2

2

At file-scope, something like

int a;

Is a declaration in the first place. It is also a tentative definition. For the same name you can have as many tentative definitions for the same name as you want iff they are technically (see the link for details) identical.

However, if you add an initialiser:

int a = 0;

You have a (regular) definition. Of these you can only have one for the same name. It must also be identical to all tentative definitions.

The second example is about scope. You can use the same name in different scopes. However, the innermost name will be used if you reference the object. There is no way to access an object with the same name in an outer scope. This is called shadowing and a properly configured compiler (i.e. enable warnings) should warn about it, but allow this.

In general this is bad coding style, because you have to check the scope when you read the code to see the declaration. (that's why the compiler should warn). Note that the inner definition need not even have the same type.

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
0

To paraphrase this answer:

In a global scope, what you have written will work fine (it will throw an error in the function scope). That is, say you write the following:

int a;
int a;
int main(...){...}

This will compile fine, but only because neither a is initialized. If you declare something with an assignment multiple times, things will break. Here is an example of this:

int a;
int a;      // No problem yet
int a = 10; // a is now initialized
int a = 12; // Error: Redefinition of a

The first two definitions (and the kind you gave) are what are known as tentative definitions. The C spec says that multiple ones of these are treated as redundant. The important thing is that each one has either zero or one definitions with initializations.

Lastly, to answer your question:

...
int a;
int a;
...   // which 'a' is visible?

My understanding is that the answer is both, since they are tentative definitions for the same variable.

Community
  • 1
  • 1
belph
  • 362
  • 2
  • 10
  • 1
    The line with `a = 11;` is a statement and no declarator; it is invalid at file-scope. Sorry, I did not read the whole answer when I wrote the first (now removed) comment. Perhaps you should concentrate on the difference between _declaration_, _tentative definition_ and (regular) _definition_; that would make things much easier to understand. – too honest for this site Nov 21 '15 at 16:39
  • My mistake. Fixed. I'll also add in a bit about those differences. – belph Nov 21 '15 at 17:14