C and pointers (pointers on C)--14th: Preprocessor

Source: Internet
Author: User
Tags switch case

14th Chapter Preprocessor


I skipped the advanced Pointers topic chapters.

Too much skill, too much discipline is not suitable for me today. But I really read and read. Let's say who reads privately and can communicate. Some tips are still very interesting.
The contents of this chapter of the preprocessor. Everyone must have used it. # # # # # # # # # # # # # #define #ifdef #undef这些, but definitely not much. As a comprehensive understanding of C, or should have a look.
The use of the preprocessor is very fastidious, the use of bad will be wrong, with good will greatly speed up the execution speed (not compile speed).


Summarize:
The first step in a C program is preprocessing. The preprocessor consists of a total of the following symbols:
1. #define
Defines a macro that replaces the action.


It links a symbolic name to a random sequence of characters.

For example, these characters may be a literal constant, an expression, or a program statement. This statement ends at the end and does not need to be;
If you are too long, you can use a backslash, such as:
#define PRINT (format,value) \
printf ("The value of" #VALUE\
"is" FORMAT "\ n", VALUE)
...
PRINT ("%d", x+3);
Output:the value of X+3 is 25.
This #value is equivalent to converting a macro parameter to a string.
Some can be implemented with a function can also be implemented with macros. The advantage of a macro is that it has nothing to do with type.

And the macro runs faster than the function. Since he does not have the overhead of returning the function call, it is ready at compile time.
The disadvantage is that the code is too long. At the same time there are non-predicted side effects, and the function is easier to predict in this regard. For example, it may run multiple times (unexpectedly) and may involve priority issues.


The difference between the two is also. Naming conventions are also different. For example, Max (A, b), and Max (A, b);


2. #ifdef #undef #endif
belongs to conditional compilation, #undef指令能够使一个原来定义好的名字被忽略.


3. #if #elif #else
These three also belong to conditional compilation. Each one is like this. Suppose (#if), if any conditions are met (#elif), assumptions are not established (#else).


Features are more powerful than #ifdef, a bit like the switch case statement.


4. #include
The implementation file includes.


There are often these two forms include "XXX.h" or # include <xxx.h> the difference is that "" is the header file under the source file path,<> is the header file under the path to the library file.
Files include the ability to nest. But no more than two floors.

Due to the danger of including the same file multiple times.
Some people say this easy. Add a:
#ifdef _headername_h
#define _headername_h
...
#endif
This often occurs in large programs today, but it adds a compile speed.


5, #error, #line, #progma
#error, an error message is generated during compilation, and the new information includes the text of your choice.
#line, tell the compiler which line number to enter next.
#progma, syntax varies by feature.


Warning:
1. Do not add a semicolon at the end of a macro definition.


2. Use the parameters in the macro definition, but forget to add parentheses around them.
Programming in principle. A macro defines the number of parentheses required.


3. Forget to add parentheses around the entire macro definition.


Both 2 and 3 cause unpredictable errors due to priority issues.


Programming Tips:
1. Avoid defining a very long sequence of code that can be implemented with a function by using the # define directive.
The length of the program will greatly increase greatly!


2. In the macro where the expression is evaluated, parentheses should be added to each occurrence of the macro parameter. Parentheses are also added to both sides of the macro definition.


3. Avoid using # define macros to create a new language.


After the creation may finally even you do not know the language is what.
4, adopt the naming convention, make the program ape very easy to see whether an identifier is a # define macro.
5, only should be appropriate to use the file included. Don't worry about its extra overhead.
This overhead is really very small and only exists with compilation. There is no in the execution.
6. Header files should include only a set of functions or arrays of declarations.
7. Separating the declarations of different sets into different header files can improve the information hiding.
8. Nested # include files make it very difficult to infer dependencies between source files.


So try to reduce the nesting of # include files.


Problem:
1, #define与字面值常量的长处.


Good readability. Ability to give constants a name that they can read.
If you want to change the value of the constant, just need to change the contents of define, convenient.
2, write a macro for debugging, print out the arbitrary expression. It should accept two of the parameters when it is called. The 1th one is the printf format code, and the 2nd is the expression that needs to be printed.


#define DEBUG_PRINT (format, value) printf ("File%s, line%d:%s =" format "\ n", \
__file__, __line__, #value, value);

Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

C and pointers (pointers on C)--14th: Preprocessor

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.