C and pointer (pointers on C) -- Chapter 2: pre-processor
Source: Internet
Author: User
Chapter 4 pre-processor
I skipped the advanced pointer topic chapter. Too many tips and too many academic discussions are not suitable for me now. But I did read it, and I didn't understand it in many places. If anyone reads it, they can talk about it in private. Some tips are very interesting.
The content of the Preprocessor chapter must have been used by everyone. What are the # include, # define # ifdef # UNDEF, but they are definitely not used much. As a comprehensive understanding of C, you should take a look at it.
The usage of the Preprocessor is very exquisite. If it is not used properly, it will lead to errors. If it is used well, it will greatly speed up the running time (not the compilation speed ).
Summary:
The first step of the C program is preprocessing. The pre-processor contains the following symbols:
1. # define
Define the macro and replace it.
It associates a symbolic name with any character sequence. For example, these characters may be a nominal value constant, expression, or program statement. This statement ends at the end, not required;
If it is too long, you can use a backslash, for example:
# 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.
# Value is equivalent to converting a macro parameter into a string.
Some functions can be implemented or implemented using macros. The advantage of macros is that they are irrelevant to types. In addition, macros are executed faster than functions. Because it does not have the overhead returned by function calls, it has been completed during compilation.
The disadvantage is that the Code is too long and has unpredictable side effects. functions are easier to predict in this respect. For example, it may be executed multiple times (unexpected) and may involve priority issues.
The two are also different in naming conventions, such as Max (A, B); and max (A, B );
2. # ifdef # UNDEF # endif
It is a conditional compilation. # The UNDEF command can ignore a defined name.
3. # If # Elif # else
These three are also Conditional compilation. They are the same. If (# If), what will happen when any conditions are met (# Elif), if none are true (# else ).
More powerful than # ifdef, a bit like switch case statement.
4. # include
Implement file inclusion.
These two forms are often used # 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 library file path.
Files can be nested, but cannot exceed two layers. This may cause the risk of multiple times containing the same file.
Some people say this is easy to add:
# Ifdef _ headername_h
# DEFINE _ headername_h
...
# Endif
This often appears in large programs, but it increases the compilation speed.
5. # error, # Line, # progma
# Error: an error message is generated during compilation. The new message contains the selected text.
# Line: Tell the compiler the row number of the next input.
# Progma, syntax varies by feature.
Warning:
1. Do not add a semicolon at the end of a macro definition.
2. Use parameters in macro definitions, but forget to add parentheses around them.
In principle, macro-defined parameters require various brackets.
3. Forget to add parentheses on both sides of the macro definition.
Both 2 and 3 may cause unpredictable errors due to priority issues.
Programming tips:
1. Avoid using the # define command to define code with a long sequence that can be implemented by functions.
The length of the program will increase greatly !!
2. In the macros that evaluate the expression, brackets should be added for each macro parameter, and brackets should be added on both sides of the macro definition.
3. Avoid using the # define macro to create a new language.
After the creation, you may not even know what the language is.
4. Using naming conventions, programmers can easily see whether an identifier is a # define macro.
5. Use File Inclusion whenever appropriate, without worrying about its additional overhead.
This overhead is really small, and it only exists and is compiled, and is not running.
6. the header file should only contain the declaration of a group of functions or arrays.
7. Separating declarations from different sets into different header files can improve information hiding.
8. nested # include files make it difficult to determine the dependency between source files.
So try to reduce the # include file nesting.
Problem:
1. # advantages of define and literal constant.
Easy to read. You can give a constant a name that you can understand.
To modify the value of a constant, you only need to modify the define content.
2. Compile a macro for debugging and print the expected expression. Two parameters should be accepted when the macro is called. The first is the printf format code, and the second is the expression to be printed.
# Define debug_print (format, value) printf ("file % s, line % d: % s =" format "\ n ",\
_ File __, _ line __, # value, value );
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