About the use of extern declaration global variables

Source: Internet
Author: User

This article is mainly about the use of extern declaration of global variables, although the knowledge point is relatively simple, but easy to make mistakes, only with those beginners C + + friends to share, hope not to let Daniel laughed.
The most basic use of extern is to declare a global variable. Here you need to pay attention to two points, one is "declaration", the other is "global variable"; Let's analyze these two concepts first.
Declaration: There is a difference between a declaration and a definition. A declaration is not equal to a definition, the declaration simply indicates the name of the variable and does not allocate storage space for it; Defines the name of the variable and assigns storage space to the variable, and the definition contains the declaration. For example:
extern int i; The variable i is declared, but no storage space is allocated and cannot be used.
int i; Defines the variable i, and allocates the space that can be used.
Note: A variable can be declared more than once in a program, but only once.
If the declaration has an initialization, it is also defined as a definition, for example:
extern int i = 5;//define Variable 5
If an extern int i = 5 is again in the following program, or a statement of int I, an error occurs because the variable can only be defined once.
Global variables: Popularly speaking, a variable defined within a function is called a local variable, whose scope is to know the end of the function from the definition, and the scope of the function defined outside it is called a global variable, and it is scoped from the definition to the end of the file.
Note: The scope starts at the definition, whether it is a global variable or a local variable. For example:
int main ()
{
cout<<i<<endl; Error, the variable i is not defined
int i=5; The scope of the variable I starts here
return 0;
}
Understanding these two concepts, we look back at the role of extern. The role of extern is to enlarge the scope of the global variable, where the scope of the global variable begins at the definition until the end of the file, and the use of an extern advance declaration becomes the beginning of the declaration, until the end of the file. So, for the above procedure, make the following change is not right.
int main ()
{
extern int i;
cout<<i<<endl;
int i=5;
return 0;
}
It's actually wrong, because, as I said before, extern is used to declare global variables, and I is a local variable, and if I define the variable I outside the main function, it is the following:
int main ()
{
extern int i; After the declaration, the scope of the variable I starts at that point until the file ends.
cout<<i<<endl;
return 0;
}
int i=5;
Use: In actual programming, sometimes the program needs to contain multiple source files, and if the files have common variables, the variable follows the form of "once defined, multiple declarations." This is defined in one file, and the other files are declared when they are used. For example, a variable is defined in the file file1:
File1:
int i = 5;
If you want to use this variable in the file File2, you can do the following:
File2
extern int i; At this point, the compiler will know that I is a variable that has been defined elsewhere, automatically in this file
or other file search
i=6; After the declaration, you can manipulate the variables in File2

(The const variable can use extern like the normal variable above, or you can use the following methods)
Note: This use is an exception to the const variable. The const type variable defaults to the local variable of the current file and cannot be used even if it is declared in other files. To be used in other files, defining the const type variable must be explicitly indicated in front of extern. For example:
File1:
extern const int i=5;
It can be declared and used in other files at this time. The reason for the opposite is related to the use of the header file, which is not detailed here.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.