C-language preprocessing commands

Source: Internet
Author: User

1. cutting-edge ansi c standards stipulate that some "preprocessing commands" can be added to the C source program to improve the programming environment and programming efficiency. These preprocessing commands are not part of the C language and cannot be compiled directly. First, we need to "pre-process" these special commands in the program. The pre-processed program does not include pre-processing commands, finally, the compiled program will perform normal compilation on the pre-processed source program to obtain the target code that can be executed.

C provides the following preprocessing functions: 1. macro Definition 2. file contains 3. conditional compilation is implemented by macro-defined commands, file inclusion commands, and Conditional compilation commands, which start.
2. macro definition 2.1 macro definition without parameters uses a specified identifier to represent a string. The general format is: # define identifier string example:
#define PI 3.14

Note:

In this program file, the specified identifier PI is used to replace the "3.1415926" string. During preprocessing, "3.1415926" is used for all PI after the command appears in the program; these identifiers are called "macro names ". Generally, macro names are represented in uppercase to distinguish them from variable names. Second, the macro definition does not check the correctness during pre-compilation. The macro definition is not a C statement and does not have to be a semicolon at the end of the row. If a semicolon is added, it is replaced together with a semicolon, which is prone to errors.

Example:

#define  PI 3.14;     ...area = 3.14*r*r;

Macro expansion, statement is
Area = 3.14; * r; // syntax error
2.2 macro definition with Parameters

The macro definition with parameters is not a simple string replacement, but also a parameter replacement. The general format is: # define macro name (parameter table) string example:
// Defines the rectangular area S. The areas a and B are variable-length # define S (a, B) a * B... area = S );
Expansion Process: If a macro (such as S (3, 2) with real parameters in the Program), replace the string specified in the # define command line from left to right. If the string contains the form parameters (such as a and B), the corresponding real parameters are replaced by the form parameters. If the character in the string is not a parameter character (such as *), it is retained. The replacement process is as follows:

Therefore, the value assignment statement is expanded
area = 3*2;
Example:

# Define PI 3.14 # define S (r) PI * (r) int _ tmain (int argc, _ TCHAR * argv []) {float a, area; a = 2; area = S (a + 1); // expand 3.14 * (a + 1) * (a + 1) printf ("r = %. 2f, area = %. 2f \ n ", a, area );}

Running result:
r=2.0,area=28.26
Note: Do not add spaces between the macro name and the brackets with parameters. Otherwise, the macro definition without parameters will become prone to errors. Example:
# Define S (r) 3.14 * (r )... Area = S (a + 1); // expand 3.14 * (r) (a + 1)


3. file Inclusion the so-called "File Inclusion" means that a source file can include all the content of another source file in this file. The # include Command provided by C language is used to implement file inclusion. The general form is as follows:
# Include <文件名> (Usually library files) # include "file name" (User-Defined files)
Image Display File Inclusion
4. Conditional compilation in general, all the rows in the source program must be compiled, but sometimes the compilation only needs to meet certain conditions. This is "Conditional compilation ". A group of statements can be compiled when a condition is met, while another group of statements can be compiled when the condition is not met. Several forms of Conditional compilation:

(1) If "identifier" is defined, "program segment 1" is executed"

# Ifdef identifier <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + kernel + ICAgICAjZW5kaWY8L3A + kernel + PC9wPgo8cHJlIGNsYXNzPQ = "brush: java;" ># ifdef COMPUTER_A # define INTEGER_SIZE 16 # endif

(2) If "identifier" is defined, execute "segment 1"; otherwise, execute "segment 2"

# Ifdef identifier

Procedure 1

# Else

Procedure 2

# Endif

Example:

#ifdef COMPUTER_A     #define  INTEGER_SIZE 16#else     #define  INTEGER_SIZE 32#endif

(3) If no "identifier" is defined, execute "program segment 1"

# Ifndef identifier

Procedure 1

# Endif

(4) If "true" executes "program segment 1", otherwise "program segment 2" is executed"

# If expression

Procedure 1

# Else

Procedure 2

# Endif

Conclusion: The pre-compilation function is unique in C language, which facilitates program portability and increases program flexibility.

Related Article

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.