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

Related searches

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 inside the main() function? It shows the following 2 errors:

1) redeclaration of 'y' with no linkage

2) previous declaration of 'y' was here

Why is it showing error for the local variable but not global variable? Not only my book, but the following 2 links from this forum clearly state that we can declare a variable multiple times (though define only once).

link1 link2

And kindly take care to explain what does the "with no linkage" part of the first error "redeclaration of 'y' with no linkage" mean? What linkage and to whom? Where would a local variable be linked?

    #include<stdio.h>

    int x;
    int x;
    int x=303;

    int main(void)
    {

      int y;
      int y;
      int y=776;   //Works fine if above 2 declarations are removed!!

      printf("The value of x is %d,and of y is %d",x,y);

    }

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.

Why C allows multiple global declarations of the same variable but , It contains well written, well thought and well explained computer science and Output in C: redeclaration of 'x' with no linkage In C, the first program fails in compilation, but second program works fine. C allows a global variable to be declared again when first declaration doesn't initialize the variable. In C, a definition of a global variable can be used for a declaration multiple times. But if the program only has extern int x;, which is a declaration, the compile will abort since there is no place where memory is allocated to the variable. – shawnhcorey Oct 25 '17 at 11:46


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.

Redeclaration of global variable in C, Local Variables # The variables which are declared inside the a = %d\n", a); // signal to operating system everything works fine return 0; } Global variables are automatically initialized to 0 at the time of declaration. Unlike local variables, global variables are not destroyed as soon as the function ends. In C,why is multiple declarations working fine for a global variable but not for a local variable? 6 In my code, why is lack of a function declaration a non-issue for one function, but throws a warning for another?


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
   }
}

Local, Global and Static variables in C, C - Scope Rules - A scope in any programming is a region of the program where a defined Let us understand what are local and global variables, and formal parameters. Local variables are not known to functions outside their own. # include <stdio.h> int main () { /* local variable declaration */ int a, b; int c; /* actual � In the following code, why does the definition of the global variable "x" show the warning "data definition has no type or storage class" but the same thing works fine for the local variable "y"?Al


C - Scope Rules, C Tutorial – Functions and Global/Local variables. Functions are often used when the same piece of code has to run multiple times. Where the functions are declared in your program does not matter, as long as The main() function starts with the declaration of three integers. Its good and easy way to understand tnx. Output in C: 5. In C, the first program fails in compilation, but second program works fine. In C++, both programs fail in compilation. C allows a global variable to be declared again when first declaration doesn’t initialize the variable.


C Tutorial – Functions and Global/Local variables � CodingUnit , Sometimes in C programming, a variable must be like cellular phone service: available everywhere. The variable also can be used by any function at any time. This type of variable could be called a universal variable. It could be called a worldwide variable. But in C, it’s referred to as a global variable.


To declare global variables in C++, we can declare variables after starting the program. Not inside any function or block. If we want to declare some variables that will be stored in some different file, then we can create one file, and store some variable.