-1

In C an extern variable gets the initial value of "zero". however after reading about this particular class category.It says that declearing variable as an extern, means the value only passed the "decleration stage".How come a variable can get an initial value without procceding to the initialization stage and allocating a memory for the particular variable? Please consider the following code:

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

I understand why a problemetic sitution arises: we tried to initialize a value to a variable which is not allocated in the memory(the variable doesn't really exist). My point is: why it's memory doesn't exist and followoing "extern's" terminology the variable has an initial value of "zero". In my mind it means that the memory does actually exist if it has an itial value....

user6394019
  • 121
  • 5
  • 3
    The memory does exist, its just somewhere else. Otherwise you'd get a linker error. – tkausl Feb 21 '19 at 23:06
  • @tkausl but if it desn't exist somewhere else? Is it set to be "zero", it's initial value or do I get a linker error? – user6394019 Feb 21 '19 at 23:09
  • The variable isn't initialised to `0` because it is `extern` but because it is a static variable and is initialised regardless of being made available to another compilation unit. – Weather Vane Feb 21 '19 at 23:11
  • @WeatherVane but as far as I am aware of... static and extern variable has the same initial value "zero". – user6394019 Feb 21 '19 at 23:13
  • @user6394019 the `extern int var; ` does not exist in the compilation unit in the question. It's a declaration of a variable which is defined in another section. If I try to compile and link the program just as it is, I get *error LNK2019: unresolved external symbol _var referenced in function _main*. – Weather Vane Feb 21 '19 at 23:14
  • @WeatherVane so when is it supposed to get an initial value of zero? I am sorry for stumpping in this particualr part. it's just doesn't make sense to me, how an extern variable is not allocated in the memory but at the same time, extern variable is known to have an initial value zero. – user6394019 Feb 21 '19 at 23:17
  • It is initialised when the program is run, either to `0` by default or to whatever you explicitly set it to at the point of its definition. For example `int var = 42;`. In your `main` the line `var = 10; ` is not initialisation: the variable has already been initialised. – Weather Vane Feb 21 '19 at 23:19
  • There is a FAQ about `extern` variables [here](https://stackoverflow.com/questions/1433204/how-do-i-use-extern-to-share-variables-between-source-files) and some links in its right-hand panel. – Weather Vane Feb 21 '19 at 23:25
  • @tkausl: The code in the question does get a linker error. – Eric Postpischil Feb 22 '19 at 00:03
  • C’s rules about what is an external definition, versus what is merely a declaration are a bit of a mess. So it is not unusual that students find them puzzling. They depend on context, and certain forms can be *tentative definitions* that cause an object to be defined only if something else doesn’t. For the code in the question, `extern int var;` is a declaration and is not a definition. So `var` is not defined, and the code in the question will not link. Further rules are in clause 6.9.2 of the C 2018 standard. – Eric Postpischil Feb 22 '19 at 00:17

1 Answers1

1

For this code to be valid, another part of the program must contain the definition of var, e.g.:

int var = 0;

which is where the initial value comes from. If you don't provide an initializer then it behaves as if you initialized with = {0} (that is the rule for static storage duration variables).

M.M
  • 138,810
  • 21
  • 208
  • 365