+ 1
Why am I not able to access the global variable using extern?
https://code.sololearn.com/cBPk9ojl6YDf/?ref=app Why isn't a value changing to 5 after using the extern keyword?
11 Respostas
+ 2
The extern keyword indicates that this variable was defined elsewhere, for example, when creating large projects, all global variables can be stored in a separate file, and subroutines are scattered across other files.
That is, extern essentially copies the variable.
This increases the compilation speed.
In your case, extern is not required, "main" and this how sees the given variable.
+ 2
Mahmoud Osman
Because you declared two different variables, one global, the other local, and it will throw an error because the local variable is not used.
Write "return printf("%d%d", a, get_a());" and there will be an output: 65
+ 1
Verstappen
Because you are re-initialising a by 6 so your output will be 6
+ 1
Vasiliy oh now I get it, thankss!!
+ 1
Verstappen, I'm glad I was able to help you, you can mark my answer as the best âșïž
+ 1
Vasiliy haha alright done!!
+ 1
đ
+ 1
Why not to :
int a = 5;
int get_a (void) {
return a;
}
int main(void) {
int a = 6;
return printf ("%d", get_a()) ;
}
+ 1
Vasiliy done, now I get it clearly
0
Jazz ohh i get it but still this can be achieved even without extern right? as a's value is going to be reinitialized again
Or is extern a keyword for declaring variables in different files? Please explain.
0
Mahmoud Osman why did you use return in main()?