Declaration and definition of variables in C + + programming and Preprocessing commands parsing _c language

Source: Internet
Author: User

About the declarations and definitions of C + + variables
As we already know, a function generally consists of two parts: the declaration part and the execution statement.

The role of the Declarations section is to describe the properties of the relevant identifiers, such as the variables, functions, structs, and so on. For functions, the distinction between declarations and definitions is obvious, as explained above, the declaration of a function is a prototype of a function, and the definition of a function is the establishment of function functions. The declaration of a function can be placed in the Declarations section, and the definition of a function is clearly not within the scope of the declaration part of a function, it is a standalone module in a file.

The relationship between a declaration and a definition is slightly more complex for a variable. There are two scenarios for variables that appear in the Declarations section: one that requires storage space (such as int A;), and the other is that there is no need to create storage space (such as extern int A;). The former is called a definition declaration (defining declaration), or simply defined (definition). The latter is called a citation statement (Referenceing Declaration). Broadly speaking, a declaration includes a definition, but not all declarations are definitions. for "int A;" It is a defined declaration, which can be said to be a declaration or a definition. for "extern int A;" It is a declaration, not a definition. Generally for the sake of narration, the Declaration of the establishment of storage space is called the definition, and the declaration that does not need to create storage space is called the declaration. Obviously the statement here refers to the narrow sense, that is, the undefined statement. For example:

int main ()
{
 extern int A;//This is a declaration is not a definition. Declaring A is a defined external variable
}
int A;//is defined, and defines a as an integer external variable

The meanings of external variable definitions and external variable declarations are different. The definition of an external variable can only be once, its position is outside all functions, and the declaration of an external variable in the same file can be multiple times, and its position can be within a function or outside of a function. The system allocates storage units according to the definition of external variables. Initialization of an external variable can only be done at the time of definition, not in a declaration. A declaration, whose function is to send a message to the compiling system declaring that the variable is an external variable defined at a later time, simply a declaration for referencing the variable in advance. extern is used only as a declaration, not as a definition.

The function of declaring a variable with static is two:
Static declarations are made to local variables so that the variable is not released after the end of this function call, and the entire program is always present during the execution of the procedure, making it the whole process of the program.
Global variables are declared with static, the scope of the variable is limited to this file module (that is, the file being declared).

Note that when you declare a variable with auto,register,static, you add these keywords on the basis of the defined variables, not on your own. such as "Static A;" is illegal and should be written as "static int A;".

C + + preprocessing commands
A number of "preprocessing commands" (preprocessor directives) can be added to the C + + source program to improve the programming environment and improve program efficiency. Preprocessing commands are uniform in C + +, but they are not part of the C + + language itself and cannot be compiled directly (because the compiler does not recognize them).

Now used C + + compiler system includes preprocessing, compilation and connection parts, so many users mistakenly think that preprocessing commands is a part of C + + language, even think they are C + + statements, this is wrong. preprocessing commands and C + + statements must be distinguished correctly to distinguish between preprocessing and compilation to correctly use preprocessing commands. One important difference between C + + and other high-level languages is the ability to use preprocessing commands and features that have preprocessing.

C + + provides the following 3 kinds of preprocessing functions:

    1. Macro definition
    2. file contains
    3. Conditional compilation

Use macros to define commands, file contains commands, conditional compilation commands, respectively. To distinguish them from general C + + statements, these commands begin with the symbol "#" and do not include a semicolon at the end.

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.