Declaration and definition of variables and preprocessing command resolution in C++ programming

  • 2020-05-05 11:31:46
  • OfStack

declaration and definition of C++ variable
As we already know, a function typically consists of two parts: the declaration part and the execution statement.

The declaration section is used for the relevant identifiers (such as the variable ? Function & # 63; Structure & # 63; Properties of shareware, etc. In the case of functions, the distinction between declaration and definition is clear. The declaration of functions is the prototype of functions, while the definition of functions is the establishment of functions. The declaration of a function can be placed in the declaration section, and the definition of a function is obviously not in the declaration section of a function, it is a separate module in a file.

For variables, the relationship between declaration and definition is slightly more complicated. There are two situations where a variable appears in the declaration section: one where the storage space needs to be established (e.g. int a;) ; The other does not require the creation of storage space (e.g. extern int a;) . The former is called a definitional declaration (defining declaration), or simply a definition (definition). The latter is called a referential declaration (referenceing declaration). Broadly speaking, declarations include definitions, but not all declarations are definitions. The "int a;" It is a defining declaration, both a declaration and a definition. And "extern int a;" It is a declaration rather than a definition. Generally, for the sake of narrative convenience, the declaration of establishing storage space is called a definition, and the declaration that does not need to establish storage space is called a declaration. It is clear that this statement is a narrow sense, that is, a non-defining statement. For example:


int main( )
{
 extern int a; // This is a declaration, not a definition. The statement a Is a defined external variable 
}
int a; //  It's the definition, the definition a Is an integer external variable 

An external variable definition and an external variable declaration have different meanings. An external variable can be defined only once, outside of all functions, while an external variable in the same file can be declared more than once, inside or outside of functions. The system allocates storage locations based on the definition of external variables. Initialization of external variables can only be done at definition time, not in a declaration. A declaration is a message to the compilation system stating that the variable is an external variable defined later, just a declaration to refer to it in advance. extern is used only for declarations, not for definitions.

Declaring a variable with static does two things:
Local variables are declared with static, so that the variable will not be released after the end of this function call, and will exist throughout the execution of the program, so that its storage period is the whole process of the program.
Global variables are declared with static, and their scope is limited to the module of the file (that is, the file being declared).

Note that when variables are declared with auto, register, static, these keywords are added to the definition of the variables, not used separately. Such as "static a;" Is illegal and should be written as "static int a;" .

C++ preprocessing command
Some "preprocessing commands" (preprocessor directives) can be added to C++ source programs to improve the programming environment and increase programming efficiency. The preprocessing command is C++, but it is not part of the C++ language itself and cannot be compiled directly (because the compiler cannot recognize them).

The current C++ compilation system includes preprocessing, compilation, and wiring, so many users mistakenly believe that preprocessing commands are part of the C++ language, or even that they are C++ statements, which is wrong. To use the preprocessing command correctly, you must distinguish between the preprocessing command and the C++ statement, and between the preprocessing and compilation. An important difference between C++ and other high-level languages is the ability to use preprocessing commands and the ability to have preprocessing.

The pretreatment functions provided by C++ are mainly as follows:

The macro definition file contains conditional compilation

Macro definition command, file containing command, conditional compilation command to achieve. To distinguish them from normal C++ statements, these commands begin with the symbol "#" and do not include a semicolon at the end.


Related articles: