In this code i
has been defined (and it has been initialized despite being extern
). Now the book I am reading says:
An extern declaration may be defined only if it appears to be outside a function.
and it offers no reason behind it.
#include<iostream>
using namespace std;
int main()
{
extern int i=898;
cout<<i<<endl;
return 0;
}
I have gone through the question (similar to this one on Stack Overflow) but the explanation doesn't appear to be clear. The question is:
How are these two definitions different at function scope:
int i=898;
extern int i=898;
In both the cases a single unit of int
size memory is being allocated. Is it due to linking error? Please answer clearly as (IMO) it hasn't been satisfactorily in older version whose reference has been used to mark this as duplicate.