Pre-processor in C ++

Source: Internet
Author: User

The C ++ pre-processors include the following 12.

# If # else # Elif # endif # ifdef # ifndef # UNDEF

# Error # line # pragma # include # define

The first line 7 is the control statement, with the focus on the next 5.

 

1. # define 

# Define Macro-name char-Sequence

# Define defines an identifier and a character sequence. Each time in the sourceProgramThis identifier is replaced by a defined character sequence.

The identifier is the macro name; the replacement process is the macro replacement.

# Define One 1
# Define Two One + one
# Define Pi 3.14
# Define Errmsg "Err !!! "
// .......
Printf (errmsg );

In macro definition, if the Character Sequence exceeds one line, you can use a backslash to continue the line at the end.

# Define Longstring "This is a very \
Very very .......\
Long   String "

Macro names do not have to be larger, but many c ++ programmers are used to using larger letters. In addition, it is often used to concentrate all macros in one or more separate header files.

The macro name can be changed to RMB. In the case of macro names, the variables used in the definition are all replaced by the variables in the actual program. This is similar to the inline function. But for define objects, the compiler only performs simple character replacement, and other operations such as type check. Use inline instead of define as much as possible.

# Include < Stdio. h >
# Define ABS (a) <0? -(A) :() // Brackets are important.

IntMain (Void)
{
Printf ("ABS of-1 and 1: % d", ABS (-1), ABS (1));
Return 0;
}

# Error

# Error Error-message // Error messages are not enclosed in double quotation marks.

# Error is mainly used in debugging to force the compiler to stop compilation. The error message is displayed along with other compiled information.

 

# Include

# Include < XX. h > // Search using the method defined by the compiler creator
// It usually starts from the project definition's many include directories.
# Include " XX. h " // Search using other methods defined
// Usually starting from the current working directory,
// If not found, continue searching according to <>

 

# Line 

# Line Number "FILENAME"

Used to change the content of _ line _ and _ file. These two identifiers are pre-defined identifiers in the compiler.

The former indicates the compiledCodeNumber of lines. The latter is a string that contains the name of the compiled source file.

Number is a new value of _ line _. filename is any valid file name identifier, which is a new value of _ file.

 

# Pragma

# Pragma is an implementation-defined instruction that is used to send various commands to the compiler. For example

# Pragma Once

#### Preprocessor Operators

### And # define are used together.

#, Stringize operator to convert the variable behind it into a string with quotation marks.

# Include < Stdio. h >
# Define Mkstr (s) # s
Int Main ( Void )
{
// I like C ++ is the S in the mkstr macro,
// The Preprocessor quotes this large string into "I like C ++"
Printf (mkstr (I like C ++ ));
Return   0 ;
}

#, Pasting operator, used to connect two symbols.

# Include < Stdio. h >
# Define Concat (a, B) a # B
Int Main ( Void )
{
Int XY =   10 ;
Printf ( " % D " , Concat (x, y )); // The pre-processor changes it to the print XY value.
Return   0 ;
}

 

Predefined macro name

_ Line _ file _ date _ time _ stdc _ cplusplus

 

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.