Next, let's talk about declaration, definition, initialization, assignment, and extern.

Source: Internet
Author: User

Although the definition is also a declaration, in order to facilitate the explanation of the problem, the Declaration below is only a simple declaration, and the definition is just a definition.
1. the terms declaration, definition, initialization, and assignment are essentially different. Although they sometimes look similar or even identical, if you do not know clearly, errors may easily occur or you do not know how to change them.
 
2. The a: extern keyword is used to indicate that this is a declaration: extern int I; variable I is a declaration. There must be a keyword extern before declaration. Without this keyword, it is not a declaration.
B: No extern is a definition. For example, int I = 9; is a definition. Note that int I is also a definition.
C: initialization is to give the variable an initial value when defining the variable, so the initialization statement must be a definition statement, but in turn it is incorrect, because it is similar to: int I; is the definition, but not initialized. Note: extern int I = 9; although the extern keyword extern is available, it is also a definition because of initialization, not declaration.
D: The value assignment statement is simple. The value assignment statement is a new value for a defined variable (no matter whether the variable is initialized or not. Note that the variables to be assigned must have been defined and declaration alone is not acceptable.
 
3. the extern keyword not only indicates that this is a declaration, but also indicates that the declared variable definition may be in other files in the program. The following code
// File1.cpp

# Include <iostream>
Using namespace std;
Extern int I; // This is a declaration that tells the compiler variable I definition may be in other source files, even if this file does not have the definition of I, you should not report an error.
Void main ()
{
Extern int I; // It is also a declaration of I. Its function is exactly the same as that of the above statement, indicating that there can be multiple declarations. In fact, only one of these two statements can be used. However, if neither of the two declaration statements is available, the compiler considers that variable I is not defined and reports an error.

I = 0; // a value assignment statement. A new value must be defined before the variable value is assigned. If the Definition Statement in the file2 file does not exist, no compilation error will occur, but the link may be wrong.
Cout <I;
}

// File2.cpp

Int I; // The first definition is a non-initialization definition (but the global variable I is actually initialized to 0 by default), and the variable I is defined in the file2 source file.
 
 
4. A program may include more than one file. The same variable (in fact, the global variable) in all files must be defined only once in total, but there can be several declarations. If the variable definition used in file A is in other files, you must add an extern declaration statement before using this variable in file, tell the compiler that the variable I used may be in another file.
5. extern int I = 9; as mentioned above, although there is extern, this is also a definition because of initialization. Similar to this extern or initialization statement, it can only appear in the global scope. If it appears inside the function, this is incorrect.
6. (conversion) a feature of the compiler: Modern compilers generally compile by file, that is, when multiple source files are compiled by themselves, they do not affect each other, as if they only have one file. As long as no errors occur during the compilation of each file, no compilation errors will occur. However, if no compilation error occurs, it does not mean that the program has no errors because a link error occurs. For example, the following two Codes
// A. cpp
Int I; // This is the definition of variable I
 
Void main ()
{
}

// B. cpp
Int I; // This is also the definition of variable I
The two files A and B do not affect each other during compilation, so there will be no errors during compilation, but this program is faulty, because the global variable I is defined twice, the following error is reported during the link:
B. obj: error LNK2005: "int I "(? I @ 3HA) already defined in A. obj
Debug/A.exe: fatal error LNK1169: one or more multiply defined symbols found
As mentioned above, each file does not affect each other during compilation. The Compiler does not think that variables not defined in this file are likely to be global variables, if it is defined in other files, an error is returned. The solution is to use extern to declare a variable and tell the compiler that the variable is not undefined, but defined in other files. Do not report an error.

 

 

Author: Gu Yue

Related Article

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.