top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration

C: Why redefining of variable is not an error?

+4 votes
134 views
#include<stdio.h>
int var;
int var;
int var;

int main(void) { 
  printf("%d \n",var); 
  return 0;
}
posted Jan 24, 2014 by Atiqur Rahman

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
True for global variable only...

3 Answers

+2 votes
 
Best answer

There are two types of definition for global variables.
1>Weak Symbol
2>Strong Symbol

At each definition it adds the symbol and its value in symbol table.

int var ; //Weak Symbol
int var = 20; //strong symbol

For weak later u can change the value in the symbol table by initializing it But after u initialized it becomes strong symbol.so u it can not change again the the value u will define it for next time.

Check these cases.
CASE 1:

    int var;
    int var;
    int var;

/* Here it will not give any error as each time it defines it replaces in the symbol table.so it takes the last value which u given but here last also u not initialized.So it will be part of BSS. */

CASE 2:

    int var;
    int var;
    int var = 20;

/* No error as 1st two is weak symbol(As not initialized) then in the third line when u initilized it replaces the value for the symbol var as 20 and it becomes strong symbol. */

CASE 3:

    int var;
    int var;
    int var = 20; /*  became strong*/
    int var = 30; /* Again u want to change the value in strong symbol */

/* Here till two line it is weak symbol But in third line u initialized so it became strong symbol and replaced the value.In 4th line again u want to change But u can not change the value in 4th line as it will not replaces for strong symbol. */

Here it will not give any error as the next u just declared a variable.
CASE 4:

    int var;
    int var;
    int var = 20; /*  became strong*/
    int var;  /* As u just declared it so it is no problem (But compiler dependent also (if they check strict common symbol it will give error)). */
answer Jan 24, 2014 by Sachidananda Sahu
Thank you for such a nice and expressive explanation.
Osum way to explain ...
+1 vote

My Guess: extern is assumed for global variables if not specified. The reason to allow duplicates is so that you can include a header that specifies the global variable, then in the source itself define it again with an initial value.

That is the reason why following code gives the error as extern is not assumed (by the way the same is not true for C++ there you will get error for the code you posted) -

int main(void) { 
  int var;
  int var;
}
answer Jan 24, 2014 by Luv Kumar
Your Guess is true only for functions not for variables, this is where this concept(extern) strongly differ.
Reference :-http://www.geeksforgeeks.org/understanding-extern-keyword-in-c/
0 votes

Both int var; are tentative definitions in C as they do not have an initializer and are not extern. You are allowed multiple tentative declarations and at most one non-tentative definition for any object in a translation unit so long as the definitions don't conflict in type or linkage.

answer Jan 24, 2014 by Atul Mishra
I was thinking as your last line ......good one Sir.
Similar Questions
+7 votes
#include<stdio.h>

int &fun()
{
   static int x;
   return x;
}   

int main()
{
   fun() = 10;
   printf(" %d ", fun());

   return 0;
}

It is fine with c++ compiler while giving error with c compiler.

+4 votes

case:1

int main(void)
{
  extern int var = 0;
  var = 10;
  return 0;
}

case:2

#include<stdio.h>
extern int a;
int main()
{
 extern int a;
 int a=34;
 printf("%d",a);
 return 0;
}

In both cases it's error why so? Curious to know exactly how declaration of variable with extern differ locally and globally ?

More informative answer is most welcome.

Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...