Preprocessing command, preprocessing

Source: Internet
Author: User

Preprocessing command, preprocessing
Reprinted from: http://www.cnblogs.com/kykuaileren/archive/2011/09/09/2172801.htmlThe pre-processor will analyze and execute all the pre-processing commands based on the pre-processing commands that have been placed in the file to modify the content of the source file, and then delete them, A conversion unit containing only C ++ statements is obtained. The pre-processing command starts "#".

 

Common preprocessing commands:

1. # include header files (header files generally contain function prototypes and global variables)

The following two forms are common:

# Include <iostream> Reference the standard header file and only search for the default directory of the standard library header file

# Include "myheader. h"

Reference the custom header file, search for the Directory of the source file being compiled, and search for the default directory containing the standard library header file when it is not found

2. # define defining symbols and macros

For example: # define PI 3.1415925 // defines the symbol constant PI as 3.1415925 without any additional points.

Before compilation, the pre-processor will traverse the code and replace it with the defined value (3.1415925) of the string PI where it deems the replacement meaningful. The PI in the annotation or string is not replaced.

# Undef deletion # define-defined symbols

For example:

# Define PI 3.14159265
......
// All PI values can be replaced with 3.14159265

# Undef PI
// No longer has the PI identifier

However, in C ++, it is best to use const to define variables. The preceding definition can be changed:

Const long double PI = 3.1415925;

In comparison, the former has no type designation, which may cause unnecessary troubles, while the latter has a clear definition. Therefore, we recommend using const to define constants in c ++.

# Define disadvantages:

1) type check is not supported

2) scope is not considered.

3) the symbol name cannot be limited to a namespace.

3. # indef, # ifndef, # endif

# Ifdef Compile the macro if it is defined.
# Ifndef Compile the macro if it is not defined.
# Endif

End Block Control

4. # if, # else

# If Code is compiled if the expression is not zero.
# Else Compile the Code as an if false option

5. # error

Output an error message

6. # pragma

Provides unconventional Control Flow Information for compilation programs

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.