Predict the output : extern keyword

1).
extern int var;
int main()
{
    var = 10;
    cout<<var;
    return 0;
}
Answer:Compiler Error

(Var is only declared and not defined (no memory allocated for it)).

2).
extern int var = 0;
int main()
{
    var = 10;
    cout<<var;
    return 0;
}
Answer: 10
(If a variable is only declared and an initializer is also provided with that declaration, then the memory for that variable will be allocated i.e. that variable will be considered as defined)

Comments

Popular Posts