Home > Back-end >  Why different global variables in C file must extern to visible, whereas function can be used in dif
Why different global variables in C file must extern to visible, whereas function can be used in dif

Time:09-16

Beginners learn extern a little confusion,
1. Global variables are globally visible, isn't it, why need extern keyword? But can not function needs?
2. In a project. What is the relationship between the c file? (sometimes feel like just a long file is divided into several short file)

CodePudding user response:

If you can also need not extern # include that file, but # include multiple files, they are different variables of the same name, so there will be conflicts, so usually use a file definition, others with
Extern
Function is the same ah, want to let other visible, also need to advance statement

CodePudding user response:

Whereas function can be used in different c file directly? -- -- -- -- -- -- -- who say of, also want to include the header file or extern

CodePudding user response:

Have file scope of identifiers (i.e., a global variable) you say you don't need to use a storage class specifier extern, it is the default extern,

In accordance with the relevant provisions, if in a function declaration is not using any of the storage class specifier, is considered using the storage class specifier extern.

CodePudding user response:

1. Global variables from the definition of life cycle before the program exits have been effective; Is not a global scope; Add reference extern identifies the global variables defined outside;
2. Multiple source files. Do not necessarily have relationship between c, with the same function with multiple functions in the same source file, this management and facilitate calls;

CodePudding user response:

"Programmers self-improvement, links, loaded with library"

CodePudding user response:

1, the function is not at all. In the c file called, also need extern declarations (or directly write function signatures, the default extern)
2, why need extern? Because modular programming needs, modular programming leads to a project there will be several. C file, each c file may depend on other variables and functions c files, the compiler compiler generates code after [pretreatment] - [assembly] - [build] - [link] is complete, it is not so smart can know A.c call variables and functions where it came from, need the programmer explicitly declared exten, 3 in a single step. Before this time c file to complete, at the end of the link stage began to find the call variables and functions
  • Related