in Programming in C
2,636 views
3 votes
3 votes

extern int var;

int main(void)

{

 var = 10;   // why it is not considered as definition in prog 

 return 0;

}

Why this is creating error saying (var is not defined).But as per rule 

-"by externing a variable we can use the variables anywhere in the program provided we know the declaration of them and the variable is defined somewhere."?

in Programming in C
2.6k views

1 comment

var = 10; without specifying type of variable we cant say that it is definition,thts why this program will give compile time error
0
0

1 Answer

6 votes
6 votes

External variable

As the name suggest , they are external to any function which means they are defined always outside of any function. Actually when you declare the external variable (declaration can be inside of any function or outside) you make a promise with your compiler that you have defined that variable somewhere outside of any function. 

first it's good to know what does it mean by Declaration and Definition.

Extern int a;  is actually a declaration. Declaration is just a hint to compiler that this kind of variable/function may be use somewhere so better you know about it now. Memory is not allocated during declaration.

Extern int a=5;  is actually definition but as its external variable, you can't use it inside any function. where ever outside you define extern variable , its responsibility of linker to find it and use it. Variables declared inside some function or block (i mean to say local variable)have No Linkage. You make then extern and put the definition outside blocks and linker will find then for those who need that extern variable at linking time.

In you program the linker won't be able to find definition of your declared extern variable (as you didn't define it outside and linker only search outside , in that way only linker works) so it's okay no problem , no error would be there (either it's bad programming) so ultimately there is no memory allocated for your variable. but the tragedy is you defined extern variable inside some function so here compiler would catch it at compiling time and will think , there is even no memory is allocated for that variable and some superman is trying to update its value. Compiler would say to itself ohh someone is making fun of me as he is saying me to update a variable which even don't exists :)

hope you get it tell me if more i can tell you.

3 Comments

In my program, I should write  :-

extern int a = 0;

this is declaration and definition too. ryt?

but can't we declare and define an extern variable seperately?
0
0

Yes that's what i'm saying. It's good to declare then and define them separately.

like

int main(void)

{

extern int a;

printf("a is = %d" ,a);

return 0;

}

int a=5;

when you do like extern int a=5 there won't be any problem but it would be bit uncanny to compiler because when you declare some variable extern , you tell compiler i will assign value somewhere else but when you defined it with declaration then it's just mean you said to compiler "Hey i will define it somewhere else and hey i defined it" it would sound uncanny to compiler so he will just generate warning like "you initialized and declared extern".

3
3
int main()
{
    extern int a = 5;
    printf("%d",a);
}
// why this code give errorbut the follwong code is not 

exetern int a =5;
int main()
{
    printf("%d",a);
}
//but this code not give error and give output 5 but above code give errorr so why we cant initialiaze
//extern variable in any function.

 

0
0