C Language Primer (16) Preprocessing process

Source: Internet
Author: User

preprocessing Process

Objective:

The preprocessing process scans the source code, makes a preliminary conversion, and generates a new source for the compiler. The preprocessing process can be seen before the source code is processed by the compiler.

In the C language, there is no intrinsic mechanism to do the following: include other source files at compile time, define macros, and, depending on the criteria, determine whether or not some code is included in the compilation. To do this, you need to use a preprocessor. Although the majority of compilers now contain preprocessor, they are generally considered to be independent of the compiler. The preprocessing process reads the source code, examines the statements and macro definitions that contain the preprocessing directives, and transforms the source code into a response. The preprocessing process also removes comments and extra white-space characters from the program.
A preprocessing directive is a line of code that begins with a # number. #号必须是该行除了任何空白字符外的第一个字符. #后是指令关键字, there are any number of whitespace characters allowed between the keyword and the # number. The entire line of statements forms a preprocessing directive that makes certain transformations to the source code before the compiler compiles it. The following is a partial preprocessing directive:

Instruction use
# NULL instruction without any effect
#include contains a source code file
#define Defining macros
#undef to cancel a defined macro
#if if the given condition is true, compile the following code
#ifdef If the macro is already defined, compile the following code
#ifndef if the macro is not defined, compile the following code
#elif if the previous # if given condition is not true and the current condition is true, compile the following code
#endif end an # if ... #else条件编译块
#error stop compiling and display an error message

One, the document contains
#include预处理指令的作用是在指令处展开被包含的文件. Inclusions can be multiple, meaning that a contained file can contain other files as well. The standard C compiler supports at least eight nested inclusions.
The preprocessing process does not check whether a file is already contained in the conversion unit and prevents multiple inclusions for it. This allows you to achieve different effects when you include the same header file multiple times, given the conditions at compile time. For example:
#define AAA
#include t.c
#undef AAA
#include t.c
To avoid the inclusion of header files that can only be included once, you can control them in the header file with compile-time conditions. For example:
/*my.h*/
#ifndef My_h
#define My_h
......
#endif

There are two formats for including header files in your program:
#include
#include my.h
The first method is to enclose the file with angle brackets. This format tells the preprocessor to search for included header files in the header files of the compiler's own or external libraries. The second method is to enclose the file in double quotation marks. This format tells the preprocessor to search for the included header file in the source code file of the currently compiled application, and then search the compiler's own header file if it is not found.
The rationale for using two different include formats is that the compiler is installed in a common subdirectory, and the compiled application is under their own private subdirectory. An application contains both the common header file provided by the compiler and the custom private header file. Two different inclusion formats allow the compiler to differentiate a common set of header files in many header files.

two, Macro
A macro defines an identifier that represents a specific content. The preprocessing process replaces the macro identifier that appears in the source code with the value of the macro definition. The most common use of macros is to define a global symbol that represents a value. The second use of a macro is to define a macro with parameters that can be called like a function, but it expands the macro at the calling statement and replaces the formal parameter in the definition with the actual argument at the time of the call.
1. #define指令
#define预处理指令是用来定义宏的. The simplest form of the instruction is: first the deity an identifier, and then give the code represented by this identifier. In the subsequent source code, this is used instead of the identifier. This macro extracts some of the global values that are used in the program and assigns some memory identifiers.
In this case, for the person reading the program, the symbol Max_num has a specific meaning, which represents the maximum number of elements the array can hold. This value can be used more than once in a program. As a convention, it is customary to always use uppercase letters to define macros, which makes it easy to distinguish between program red macro identifiers and generic variable identifiers. If you want to change the size of the array, you only need to change the macro definition and recompile the program.
The value represented by a macro can be a constant expression that allows the inclusion of a previously defined macro identifier. For example:
#define ONE 1#define, 2#define three (One+two)
Note that the above macro definition uses parentheses. Although they are not necessary. But in the light of caution, parentheses should be added. For example:
Six=three*two;
The preprocessing process converts the preceding line of code into:
six= (one+two) *two;
If there is no that parenthesis, it is converted into six=one+two*two;
A macro can also represent a string constant, for example:
#define Version version 1.0 Copyright (c) 2003
2. # define directive with parameters
Macros and function calls with parameters look somewhat similar. See an example:
#define CUBE (x) (x) * (x) * (x)
can be any numeric expression or even function call in place of the parameter x. Once again, you are reminded of the use of parentheses. Macro expansion is fully enclosed in parentheses, and the parameters are enclosed in parentheses, which guarantees the integrity of the macros and parameters. Look at a usage:
int num=8+2;volume=cube (num);
Expanded to (8+2) * (8+2) * (8+2);
Without those parentheses it becomes 8+2*8+2*8+2.
The following usage is not secure:
Volume=cube (num++);
If cube is a function, the above notation can be understood. However, because Cube is a macro, it can have side effects. The wipes here are not simple expressions, they will produce unexpected results. They unfold after this:
Volume= (num++) * (num++) * (num++);
It is clear that the result is 10*11*12, not 10*10*10;
So how to use cube macro safely? You must move actions that may have side effects to the outside of the macro call:
int num=8+2;volume=cube (num); num++;

3. #运算符
The # operator that appears in the macro definition converts the argument followed by to a string. Sometimes the # of this usage is called the string operator. For example:
#define PASTE (n) adhfkj#n
Main () {printf (%s\n,paste (15));}
The # operator in the macro definition tells the preprocessor to convert any arguments in the source code that are passed to the macro into a string. So the output should be adhfkj15.

4.# #运算符

# #运算符用于把参数连接到一起. The preprocessor appears in the # #两侧的参数合并成一个符号. Look at the following example:
#define NUM (a,b,c) a# #b # #c
#define STR (A,b,c) a# #b # #c
Main () {printf (%d\n,num);p rintf (%s\n,str (AA,BB,CC));}
The output of the final program is:
123
Aabbcc
Don't worry, few programmers will know # #运算符 unless you need it or the macro is used exactly as it is at hand. Most programmers never use it.

iii. conditional Compilation directives
Conditional compilation directives will determine which code is compiled and which is not compiled. You can determine the compilation criteria based on the value of the expression or whether a particular macro is defined.
1. #if指令
#if指令检测跟在制造另关键字后的常量表达式. If the expression is true, compile the following code, knowing that #else, #elif或 #endif are present, otherwise it will not compile.
2. #endif指令
#endif用于终止 # if preprocessing directives.
#define DEBUG 0
Main () {#if debugprintf (debugging\n); #endifprintf (running\n);}
Since the program definition debug macro represents 0, the # if condition is false and the subsequent code is not compiled until #endif, so the program outputs running directly.
If you remove the # define statement, the effect is the same.
3. #ifdef和 #ifndef
#define DEBUG
Main () {#ifdef debugprintf (yes\n), #endif #ifndef debugprintf (no\n); #endif}
#if defined is equivalent to #ifdef; #if!defined equivalent to #ifndef
4. #else指令
After #else指令用于某个 the # if directive, the code that follows #else is compiled when the condition of the previous # if directive is not true. #endif指令将中指上面的条件块.
#define DEBUG
Main () {#ifdef debugprintf (debugging\n); #elseprintf (not debugging\n); #endifprintf (running\n);}
5. #elif指令
#elif预处理指令综合了 the role of #else and # if directives.
#define
Main () {#ifdef oneprintf (1\n), #elif defined twoprintf (2\n); #elseprintf (3\n); #endif}
The program is well understood and the final output is 2.

6. Some other standard directives
#error指令将使编译器显示一条错误信息, and then stop compiling.
#line指令可以改变编译器用来指出警告和错误信息的文件号和行号.
#pragma指令没有正式的定义. The compiler can customize its purpose. A typical use is to prohibit or allow some annoying warning messages.

C Language Primer (16) Preprocessing process

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.