Chapter 14: Preprocessor
GitHub
Link: ch14. Preprocessor
C
Lot, take a look at the macro language used in stdio.h
applications other header files or open source projects to understand the various macro definition, pit also find many, need to learn and pay attention.
Summary of this chapter and points to note
Answers to some after-school exercises
14.9 Problem
-
Previously I did not know ... in
demo01.c
the large and small dots This chapter briefly knocked. These 5 macros are really easy to use. The specific reference answer can be seen: -
See the name knows the meaning. It is convenient to modify and maintain.
-
Test
__FILE__
macro and__LINE__
use, and a method parameter string transfer, before adding the parameter#
to. Seedemo02.c
. -
a. 3 2 3 b. 5 3 5 c. 2 4 20 d. 2 4 12. General questions.
-
It is often called, so speed is the most important thing. Using functions is expensive, so use macro definitions to improve speed.
-
This did not understand
process
What do you mean? The purpose is to find the suffix sum from any point in the array to the end point? I feel that the answer is not clear enough and a bit far-fetched... Reference answer: -
This macro obviously has side effects. This dependency is very dangerous and can easily cause an out-of-bounds situation. It should be an odd-numbered array element that will cause an out-of-bounds situation. Why do this to seek
sum
it? -
That's right. This nested definition has a limit on the number of levels, and this limit will not be reached here. First of all, here is a conditional compilation instruction, no matter which file is included, first define its own symbol and then include the other one. When it tried to include the first one again, the entire file was skipped.
-
sizeof()
It cannot be used as a constant expression here, and its value is calculated after the preprocessor is completed. Another method is to use thelimits.h
values defined in the include file.
14.10 Programming Exercises
-
The answer to
# define OK 1
a good show gas ah ... why my compiler automatically indent ... simple design can be. See reference answer: -
#if
And#elif
used in conjunction, the last fill a#else
can. In fact, feel andif-else
nested as a hair ah. Seedemo03.c
.
Essay
P291-P292
In #progma
this sense ... I do not know what I've seen #pragma
and #pragma once
ha ha.
Macros are often used in large projects. Some small open source libraries and open source projects have various macro definitions... This knowledge is not difficult, but obviously, when I practice out of class, the amount of code is all There are fewer, and fewer macros are used, so this piece of knowledge still needs to be paid attention to.
Conditional compilation is often used to comment code...it is also a good choice to debug code.
The difference between macros and functions and the comparison of their advantages and disadvantages are important:
doubt
-
P285 #undef
It states that the name needs to be redefined and needs to be removed and then redefined. In fact, it can be compiled, but here is a warning. The macro with the same name will be overwritten with the latest definition. -
Preprocessor symbol. The symbols defined by the five preprocessors are very easy to use, just pay attention.
-
The command line definition is a bit pretty,
-D
and you don’t need to enter the code yourself to modify the options.-Uname
The program willname
initially defined is ignored, typically used in conjunction with conditional compilation. -
The nested use of conditional compilation is too little.
-
File inclusion, and nested file inclusion.
#pragma once
The guarantee is only declared once. -
#line
The black technology, to modify__FILE__
and__LINE__