Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error : Redeclaration with no linkage

Tags:

c

#include<stdio.h>
int x=13; // forcing space allocation 
int x; 
int main(){
  printf("%d\n",x); 
}

The code above compiles but the one below does not. why ?

#include<stdio.h> 
int main(){
  int x=13; // forcing space allocation 
  int x;
  printf("%d\n",x); 
}

i was told that int x ; can be interpreted by the complier as a declaration or definition depending on the context . i can see that in the first case(global one) but what happens in the second.

like image 652
Bazooka Avatar asked Nov 14 '11 16:11

Bazooka


1 Answers

Quoting:

You can't have two global variables with the same name in C program. C might allow multiple definitions in the same file scope through the tentative definition rule, but in any case all definitions will refer to the same variable.

like image 154
Igor Avatar answered Oct 13 '22 23:10

Igor