In C,why is multiple declarations working fine for a global variable but not for a local variable?

后端 未结 3 1061
面向向阳花
面向向阳花 2020-11-30 09:12

In the following code, why do multiple declarations (and one definition) work fine for a global variable x but not for a local variable y which is

相关标签:
3条回答
  • 2020-11-30 09:40

    This is the way defined in the C99 standard, section 6.2.2, part 2:

    In the set of translation units and libraries that constitutes an entire program, each declaration of a particular identifier with external linkage denotes the same object or function. Within one translation unit, each declaration of an identifier with internal linkage denotes the same object or function. Each declaration of an identifier with no linkage denotes a unique entity.

    The "global" variables x have external linkage, so they denote the same object. The local y variables, on the other hand, has no linkage, so there is a collision.

    References: C99 Standard.

    0 讨论(0)
  • 2020-11-30 10:01

    In C and C++, int y; within a function is both a declaration and a definition.

    In C, int x; in the file scope (outside any function) is a declaration and a tentative defintion. Multiple tentative definitions are allowed; only one definition is allowed.

    0 讨论(0)
  • 2020-11-30 10:04

    With external variables, any declaration that isn't an initialisation is a tentative definition. These by themselves don't create any storage, so multiple ones are allowed. So taking your example:

    int x;      // tentative def
    int x;      // and again -- ok
    int x=303;  // definition -- ok
    int x=303;  // multiple definition -- error
    

    If at the end of the file there have been only tentative definitions, the variable is defined once, and set to 0.

    This means that if you link to another file which also has a tentative definition of x, you'll have an error according to the standard. Most compilers/linkers have always allowed this, though, and it's defined in the standard as an extension.

    With local variables, each declaration is a definition because of scope rules. This, however, is allowed:

    void func(void)
    {
       int y = 0;
       {
          int y = 1;   // a completely different y
       }
    }
    
    0 讨论(0)
提交回复
热议问题