[C and pointer] ch14. Preprocessor

Chapter 14: Preprocessor

GitHubLink: ch14. Preprocessor

CLot, take a look at the macro language used in stdio.happlications 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

Insert picture description here
Insert picture description here
Insert picture description here


Answers to some after-school exercises

14.9 Problem

  1. Previously I did not know ... in demo01.cthe large and small dots This chapter briefly knocked. These 5 macros are really easy to use. The specific reference answer can be seen:

    [External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-ZECDAf4e-1609241380564)(https://raw.githubusercontent.com/Y-puyu/picture/main/images /20201229140655.png)]

  2. See the name knows the meaning. It is convenient to modify and maintain.

  3. Test __FILE__macro and __LINE__use, and a method parameter string transfer, before adding the parameter #to. See demo02.c.

  4. a. 3 2 3 b. 5 3 5 c. 2 4 20 d. 2 4 12. General questions.

  5. It is often called, so speed is the most important thing. Using functions is expensive, so use macro definitions to improve speed.

  6. This did not understand processWhat 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:

    [External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-c8VsSM85-1609241380566)(https://raw.githubusercontent.com/Y-puyu/picture/main/images /20201229142135.png)]

  7. 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 sumit?

  8. 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.

  9. 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 the limits.hvalues defined in the include file.

14.10 Programming Exercises

  1. The answer to # define OK 1a good show gas ah ... why my compiler automatically indent ... simple design can be. See reference answer:

    [External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-nutbAYAh-1609241380569)(https://raw.githubusercontent.com/Y-puyu/picture/main/images /20201229143346.png)]

  2. #ifAnd #elifused in conjunction, the last fill a #elsecan. In fact, feel and if-elsenested as a hair ah. See demo03.c.

Essay

P291-P292In #progmathis sense ... I do not know what I've seen #pragmaand #pragma onceha 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:

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-35OgJQxz-1609241380570)(https://raw.githubusercontent.com/Y-puyu/picture/main/images /20201229133555.png)]

doubt

  1. P285 #undefIt 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.

  2. Preprocessor symbol. The symbols defined by the five preprocessors are very easy to use, just pay attention.

  3. The command line definition is a bit pretty, -Dand you don’t need to enter the code yourself to modify the options. -UnameThe program will nameinitially defined is ignored, typically used in conjunction with conditional compilation.

  4. The nested use of conditional compilation is too little.

  5. File inclusion, and nested file inclusion. #pragma onceThe guarantee is only declared once.

  6. #lineThe black technology, to modify __FILE__and__LINE__

Guess you like

Origin blog.csdn.net/yl_puyu/article/details/111935603