C ++ pre-processing commands and pre-processing commands

Source: Internet
Author: User

C ++ pre-processing commands and pre-processing commands

A pre-processing statement is composed of a series of pre-processing-related command operators. the pre-processing statement starts with #, followed by the keyword of the pre-processing command, followed by space, and followed by the content of the pre-processing command. C ++ provides a variety of preprocessing functions, such as macro definition, file inclusion, and Conditional compilation.

 

# Define

At the beginning of this tutorial, we have mentioned a preprocessing command: # define, which can be used to generate macro-defined constants (defined constantants or macros) in the form:

# Define name value

Its function is to define a macro definition called name, and then it will be replaced by value whenever the name is encountered in the program. For example:

# Define MAX_WIDTH 100
Char str1 [MAX_WIDTH];
Char str2 [MAX_WIDTH];

It defines two strings that can store up to 100 characters.

# Define can also be used to define macro functions:

# Define getmax (a, B) a> B? A: B
Int x = 5, y;
Y = getmax (x, 2 );

After this code is executed, the value of y is 5.

# Undef

# Undef completes the opposite of # define, which cancels the macro definition of the input parameter:

# Define MAX_WIDTH 100
Char str1 [MAX_WIDTH];
# Undef MAX_WIDTH
# Define MAX_WIDTH 200
Char str2 [MAX_WIDTH];
# Ifdef, # ifndef, # if, # endif, # else and # elif

These commands can cause some programs to be ignored under certain conditions.

# Ifdef enables a program to be compiled only when a specified constant has been defined, regardless of the defined value. Its operation is:

# Ifdef name
// Code here
# Endif

For example:

# Ifdef MAX_WIDTH
Char str [MAX_WIDTH];
# Endif

In this example, the statement char str [MAX_WIDTH]; is considered by the compiler only when the macro-defined constant MAX_WIDTH has been defined, regardless of its value. If it has not been defined, this line of code will not be included in the program.

# Ifndef: the code between the instruction # ifndef and # endif is compiled only when a constant is not defined. For example:

# Ifndef MAX_WIDTH
# Define MAX_WIDTH 100
# Endif
Char str [MAX_WIDTH];

In this example, if MAX_WIDTH is not defined when the code is processed, it is defined as 100. If it has been defined, it will keep the original value (because the # define statement line will not be executed ).

The command # if, # else and # elif (elif = else if) are used to compile the program that follows the command only under certain conditions. These conditions can only be constant expressions, such:

# If MAX_WIDTH> 200
# Undef MAX_WIDTH
# Define MAX_WIDTH 200

# Elsif MAX_WIDTH <50
# Undef MAX_WIDTH
# Define MAX_WIDTH 50

# Else
# Undef MAX_WIDTH
# Define MAX_WIDTH 100
# Endif

Char str [MAX_WIDTH];

Pay attention to how the series of commands # if, # elsif and # else end with # endif.

# Line

When we compile a program, if an error occurs, the compiler will display the name of the error file and the number of errors in the file before the error.

Command # line allows us to control these two points, that is, when an error occurs, the number of lines in the file and the file name we want to display. The format is:

# Line number "filename"

Here number is the number of new rows that will be assigned to the next row. The number of rows following it increases one by one.

Filename is an optional parameter used to replace the file name displayed when an error occurs after this line until another # line command replaces it or until the end of the file. For example:

# Line 1 "assigning variable"
Int ?;

This code will generate an error in the file "assigning variable", line 1.

# Error

This command will interrupt the compilation process and return the error information defined in a parameter, for example:

# Ifndef _ cplusplus
# Error a c ++ compiler is required
# Endif

In this example, if _ cplusplus is not defined, the compilation process will be interrupted.

# Include

We have seen this command many times. When the Preprocessor finds a # include command, it replaces this statement with all the content of the specified file. There are two methods to declare an object:

# Include "file"
# Include <file>

The only difference between the two expressions is the path under which the compiler should look for the specified file. In the first case, the file name is written in double quotation marks. The Compiler first looks for the file in the directory containing the instruction. If the specified file cannot be found, the compiler goes to the configured default path (that is, the standard header file path) for searching.

If the file name is in angle brackets, the compiler will look for it directly under the default standard header file path.

# Pragma

This command is used to configure the compiler. It varies with the platform and compiler you are using. For more information, see your compiler manual.

If your compiler does not support a specific # pragma parameter, this parameter will be ignored and will not produce errors.

Predefined macro names)

The following macro names are defined at any time:

 

Macro Value
_ LINE __ An integer that indicates the number of rows being compiled in the source file.
_ FILE __ String, indicating the name of the compiled source file.
_ DATE __ A string in the format of "Mmm dd yyyy", which stores the start date of compilation.
_ TIME __ A string in the format of "hh: mm: ss", which stores the start time of compilation.
_ Cplusplus Integer. All C ++ compilers define this constant as a value. If the compiler fully complies with the C ++ standard, its value should be equal to or greater than 199711L. The specific value depends on which version of the compiler complies.

For example:

// Standard macro name
# Include <iostream>
Using namespace std;

Int main ()
{
Cout <"This is the line number"
<_ LINE __;
Cout <"of file" <_ FILE __
<". \ N ";
Cout <"Its compilation began"
<_ DATE __;
Cout <"at" <_ TIME _ <". \ n ";
Cout <"The compiler gives"
<"_ Cplusplus value"
<_ Cplusplus;
Return 0;
}

Output result:

This is the line number 7 of file/home/jay/stdmacronames. cpp.
Its compilation began Nov 1 2005 at 10:12:29.
The compiler gives a _ cplusplus value of 1

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.