1. Let's talk about the role of "# If 0/# If 1... # endif" first. You have mentioned a little bit above, but you have not mentioned any key points. We know that the C standard does not provide single-line style annotations such as "//" in C ++, but only provides block annotation functions such, we usually use it to write descriptive comments (comments) in the Code and disable the visibility (blocking) of a code segment on the compiler during debugging. Of course, the so-called "annotation effect" and "Blocking Effect" are our subjective definitions of functions. There is no difference between the two for the pre-processor. For the former, because there will no longer be "Comments" or "code segments to be shielded" in "Comments", there will be no nested requirements, so there is usually no problem; for the latter, when we need to "block" a piece of code during program debugging, the Code may contain the aforementioned "comments" and/or "blocked sections ", in this case, "/*
*/"Nested requirements, but sb's c Standard just does not allow us to do this. When you try to use the nested block annotation function, you will find that the pre-processor processes the content between the beginning of the outermost annotation and the end of the innermost annotation into the annotation, the subsequent content that ends with the end of the outermost comment is treated as "valid code"-This obviously causes several syntax errors and causes compilation to stop. Experts brainstormed and Thought of "# If 0... # endif ", which is also processed by the pre-processor and can also block a piece of code. You can also write the instructions in it, these are the same as "/**/", but they are different: First, it allows nesting (the maximum number of layers is determined by the pre-processor), and second, you can set "# If
Change "0" to "# If 1" to cancel the "blocking" of a piece of code-excellent features, and quickly discard the clumsy! Its only drawback is that there is no "comment" text display style in the editor.
2. Now let's talk about the automatic variable localization problem mentioned on the second floor:
------------------------------------------------
Google learns:
#if 0
code
#endif
(1) Some debugging code is defined in the Code, and the code is completely ignored by the compiler. If you want the code to take effect, you only need to change # If 0 to # If 1
(2) # Another important purpose of if 0 is to be used as a comment. If the program you want to comment on is very long, # If 0 is the best at this time, so as to ensure that no mistakes will be made. (But Lin's book says never use # If 0 as a block comment)
# If 1 can make the variables in it a local variable.
(3) This structure indicates the code you wrote earlier. If you don't want to delete it, you can use this method, which is more convenient than annotation.
------------------------------------------------
Through the first point I mentioned above, we can know that the processing of "# If/# endif" is completed by the pre-processor, the only task of the pre-processor is to "pre-process" the "text replacement". It is not responsible for constant symbol table generation, variable storage space allocation, and code relocation, how can we control the lifetime of a variable and determine where the variable can be defined by "# If/# endif" for the Preprocessor ??
Open the editor and write the following code:
===========================
volatile unsigned char a;
unsigned int main(void)
{
a = 0;
#if 1
unsigned char *p = &a;
*p = 255;
#endif
return 0;
}
===========================
After saving the. c file and compiling it with any C compiler (excluding the C ++ compiler), an error will be reported on the line of the red letter-why? When the pre-processor performs "preprocessing", it finds that the if condition expression is "true", so it leaves the block of code, instead, only the "pre-processing indicator (or pre-processing command)" before and after the code block is cleared. In this way, the pre-processing result is handed to the compiler for "Translation, it finds the internal expression statement in a function (here it is the main function) (here it is a = 0 ;) then we found the variable Declaration/Definition Statement (here is the statement that declares and defines the pointer Variable P) -- according to the compiler rules, this is an error, therefore, it rejects such input, strikes, and starts to complain (abort compilation and give error messages ).
So far, all I have to say about "# If 1/# If 0/# endif" is finished. Thank you for making a brick ~ _~
Let's say a few more words.
--------------------------
So can we enjoy the "put the definition of variables in the place where they are needed" treatment that C ++ programmers get?
The answer is yes.
Means?
-- Code block!
The header of a block enclosed in braces can declare/define variables. The scope of these variables is limited to the inside of the block. Of course, these variable Declaration/definition statements must all be before the first expression statement in the code block. Otherwise, the error described above will appear. Fortunately, the C compiler allows us to nest the definition code block in the code block, therefore, from now on, you do not need to define a lot of variables at the beginning of the function, where you only need to write Declaration/definition statements of the variables required during the entire function execution process; as for loop variables in the loop structure and other variables with low lifetime, their declaration/definition statements can be placed in the header of a code block enclosed by a pair of braces.
With so many worships, I wrote a short piece of code to prove my argument. Haha:
======================================
Volatile unsigned char;
Unsigned int main (void)
{
A = 0;
{// Note
Unsigned char * P = &;
* P = 255;
}
Return 0;
}
======================================
Note: The Writing Style of my personal code is that braces exclusively occupy one line and the indentation is the same as that of the parent statement. The statements in function body, loop body, case, and if increase the indentation, but the above "code block" is not added.
Storage and compilation.
Hmmmm: the compiler no longer prompts errors in the Red Line. The compiler has passed the verification ~ _~