[Mac-10.7.1 lion intel-based]
Q: What does preprocessing do?
A: pre-processing, as the name implies, pre-processing. Header files in the source code, macros, and Conditional compilation are all handled during preprocessing. In other words, a statement starting with # is called preprocessing. However, if # is enclosed in quotation marks, it is simply a character or string.
Q: How to prove the existence of preprocessing?
A: The following code is saved as macro. C:
#include <stdio.h>#define NUM 100int main(){ printf("%d\n", NUM); return 0;}
Use gcc-e macro. C-o Macro. I to obtain the preprocessing result (due to space issues, only the last few lines of code are intercepted ):
# 500 "/usr/include/stdio.h" 2 3 4# 2 "macro.c" 2int main(){ printf("%d\n", 100); return 0;}
As you can see, the num in the source code has been replaced with the macro 100.
In fact, Macros in Preprocessing can also be specified in the command line. The following code is saved as macro. C:
#include <stdio.h>int main(){ printf("%d\n", NUM); return 0;}
We can see that the num in the Code is not defined, and then use the compilation command to add the macro definition for the num:
Gcc-dnum= 100 Macro. C-o macro
Compilation is complete, no problem, and the operation is OK.
Similarly, for header file inclusion and Conditional compilation, you can use the preprocessing command to obtain the processed code form, which will better understand the meaning of preprocessing. Debugging macros is not a simple task. If it is difficult to determine whether a macro is useful or what the corresponding macro string is, it is a good way to use the preprocessing command to get the result.
Q: What is the meaning of a string with a # symbol in front of it?
Shape:
#define PRINT_CH(ch) printf(#ch" is %c\n", (ch));
A: converts the character combinations to the corresponding string format.
Use the following code to save it as macro. C:
#include <stdio.h>#define PRINT_CH(ch) printf(#ch" is %c\n", (ch));int main(){ PRINT_CH('a') return 0;}
Run the GCC-e-o Macro. I Macro. c compilation command to obtain the pre-processed file macro. I.
Run the following command to obtain the last 10 lines of the preprocessing file:
The print_ch ('A') macro is processed as: printf ("'A'" "is % C \ n", ('A '));
# Ch is used to make it look like "ch. The combination of two strings is equivalent to String concatenation. Compilation and running results:
Q: What is the meaning of the # symbol in addition to the # symbol above?
A: It indicates parameter connection. The connection process is simple and you cannot see any changes. For example:
#include <stdio.h>#define CATENATE(a, b) a##bint main(){ int ab = 1; printf("%d\n", CATENATE(a, b)); return 0;}
Save as preprocess. C. The result after preprocessing is (only the last few lines of code are truncated ):
# 2 "preprocess.c" 2int main(){ int ab = 1; printf("%d\n", ab); return 0;}
Compile and run:
We can find that the result of a # B is the AB symbol.
Q: The macro definition can also be used to continue the rows. Why is it still wrong to use the backslash to continue the rows?
A: This may be caused by the fact that the newline character is used in the string literal. This is not allowed; or the newline character is followed by a non-newline character. The meaning of the continued line is to remove the next line break character, as it did not happen, if it is not a line break character, it may cause an error. However, GCC 4.2 is not very strict with this requirement. The following code is saved as preprocess. C:
#include <stdio.h>#define ADD(a, b) \ ((a) + (b))int main(){ int ret = ADD(1, 2); printf("%d\n", ret); return 0;}
There is a space next to the last row of the add macro, which may cause compilation errors in some compilers.
Run cat-e preprocess. C to check whether there is any space behind the line feed:
We can see that there is a space behind the line break of the second line. Compile it. There is no problem.
Q: I can see that the printf function is a variable parameter. Can it be defined using a macro?
A: Yes. _ Va_args _ represents a variable parameter. The following code:
#include <stdio.h>#define printf_ex(...) printf(__VA_ARGS__)int main (int argc, const char * argv[]){ printf_ex("hello%d\n", 1); return 0;}
The Code macro above defines the printf_ex function. The parameter is a variable parameter, and its implementation is the printf function, __va_args _ is a variable parameter of printf_ex.
Compile and run:
Q: Since macro definition is supported, what is the result of repeated macro definition?
A: In this case, the pre-processor generally follows the definition of the latter, but the general pre-processor will also issue a warning.
The following code is saved as redefinition. C:
#include <stdio.h>#define A 10#define A 11#define PRINT_D(intValue) printf(#intValue" is %d\n", (intValue));int main (int argc, const char * argv[]){ PRINT_D(A) return 0;}
Compile:
A duplicate macro definition warning is reported.
Run:
Q: Sometimes I find that I need to print the current running position, such as the file name, number of rows, and function where the running is located. What should I do?
A: You can use the predefined symbols _ file __, _ line _, and _ FUNC. Here we use _ FUNC _ as an example:
#include <stdio.h>int add(int a, int b){ printf("func %s execute begin...", __func__); return a + b;}int main (int argc, const char * argv[]){ add(1, 2); return 0;}
Compile and run:
The _ FUNC _ In the Add function is replaced with ADD.
Xichen
16:30:10