Why does declaring an external variable inside main () work, but not defining it inside main ()?

This seems very trivial, but a somewhat rigorous explanation of the following behavior will help my understanding extern lot. Therefore, I will be grateful for your answers.

In the following sample program, I declared the extern x variable inside the function ( main() ). Now, if I define a variable in the file scope immediately after main() and assign 8 then the program works fine and prints 8 . But if I define the variable x inside main() after printf() , expecting the extern declaration to reference it, then it fails and gives the following error:

 test.c||In function 'main':| test.c|7|error: declaration of 'x' with no linkage follows extern declaration| test.c|5|note: previous declaration of 'x' was here| ||=== Build finished: 1 errors, 0 warnings ===| #include<stdio.h> int main() { extern int x; printf("%d",x); int x=8; //This causes error } //int x=8; //This definition works fine when activated 

I see only one error in the code that the int x=8 operator means that we declare x again as a variable with the storage class auto . I do not understand. Can you tell me the following:

1) Why are we allowed to declare extern variable internally without any warnings or errors? If it is valid, what exactly does this mean?

2) . Since we declared x as extern inside the function and did not detect errors, why then this expression does not refer to the definition of the variable inside the function, but looks outside when the variable is defined outside? Is a conflicting auto-vs-extern storage class declaration causing this?

+3
source share
4 answers

extern A variable declaration is a promise to the compiler that the definition of a global variable will be somewhere else. Local variables do not qualify as fulfilling a promise to the compiler because they are invisible to linkers. In a sense, extern declarations are similar to declarations in direct expression of functions: you tell the compiler "I know that this function exists, so let me use it now and let the linker take care of finding the actual implementation."

+5
source
 Why are we allowed to declare an extern variable inside a function,without any warning or error?If valid,what exactly does it mean? 

Ans: - we can use extern at the functional level to expose it only during the duration of this function.

 Since we declared x as extern inside the function and it showed no error,why then this declaration doesn't link to the definition of the variable inside the function,but looks outside,when the variable is defined outside? Is conflicting storage-class declaration auto-vs-extern the reason for this? 

Ans: Variables declared in a block scope (for example, local variables) do not have a binding unless they are explicitly decalized as extern.

0
source

just remember the concept that when we declare a variable as extern inside a function, we can only define it outside that function.

0
source

No one uses extern in this way. extern is usually used for large projects, a large number of .c, .h files and some variables should be used together. In these circumstances, compilation often does not allow the declaration of a variable (perhaps it is in some .h file that is not compiled yet), then "extern" is used to say that he left it at the moment and proceed to compile, this the thing will be considered in the binding phase.

-1
source

Source: https://habr.com/ru/post/946301/


All Articles