Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

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

    }
like image 845
Rüppell's Vulture Avatar asked Mar 31 '13 21:03

Rüppell's Vulture


People also ask

Can a variable have multiple declarations in C?

Every declaration should be for a single variable, on its own line, with an explanatory comment about the role of the variable. Declaring multiple variables in a single declaration can cause confusion regarding the types of the variables and their initial values.

What is the difference between local variable and global variable in C?

The main difference between Global and local variables is that global variables can be accessed globally in the entire program, whereas local variables can be accessed only within the function or block in which they are defined.

What are two reasons why you should not use global variables?

A global variable can have no access control. It can not be limited to some parts of the program. Using global variables causes very tight coupling of code. Using global variables causes namespace pollution.

What is the drawback of declaring variables as global?

Disadvantages of using Global VariablesToo many variables declared as global, then they remain in the memory till program execution is completed. This can cause of Out of Memory issue. Data can be modified by any function. Any statement written in the program can change the value of the global variable.


1 Answers

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
   }
}
like image 136
teppic Avatar answered Oct 13 '22 01:10

teppic