Pre-C + + preprocessing directives

Source: Internet
Author: User

    1. Preprocessing Directives preprocessor directives
      1. Define
      2. Undef
      3. Ifdef ifndef if endif else and elif
      4. Line
      5. Error
      6. Include
      7. Pre-defined identifiers
      8. pragma
preprocessing directives (preprocessor directives)

preprocessing directives are the commands we write to the preprocessor (preprocessor) in the program code, not the statements of the program itself . the preprocessor is automatically executed by the compiler when we compile a C + + program, which controls the first validation and digestion of program code.

All of these instructions must be written in a separate line, and they do not need to be terminated (;) semicolon.

#define

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

#define Name Value

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

#define MAX_WIDTH 100
Char Str1[max_width];
Char Str2[max_width];

It defines two strings that can store a maximum of 100 characters.

#define can also be used to define macro functions:

#define GETMAX (a) (a) > (b) ( A):(B))
int x=5, y;
y = Getmax (x,2);

After this code executes, the value of Y is 5.

#undef

#undef complete with the #define相反的工作, it cancels the macro definition for the passed-in parameter:

#define MAX_WIDTH 100
Char Str1[max_width];
#undef max_width
If you use the Max_width compiler after this code, you will get an error.

#ifdef, #ifndef, #if, #endif, #else and #elif

These directives allow a part of the program to be ignored under certain conditions.

#ifdef allows a program to be compiled only if a specified constant has already been defined, regardless of what value is defined. 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]; It is only considered by the compiler if the macro definition constant max_width is already defined, regardless of its value. If it is not yet defined, this line of code is not included in the program.

#ifndef has the opposite effect: the code between the instruction #ifndef and the #endif is compiled only if 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 it is processed to this code, it is defined as a value of 100. And if it is already defined, it will keep the original value (because this line of # define statements will not be executed).

Command # If, #else and #elif (elif = else if) are used to make the part of the program followed by it be compiled only under certain conditions. These conditions can only be constant expressions , for example:

const int Value_judge = #if Value_judge >200
#define MAX_WIDTH 200

#elif Value_judge <50
#define MAX_WIDTH 50

#else
#define MAX_WIDTH 100
#endif

Char Str[max_width];

Watch out for this series of instructions #if how #elif and #else end with #endif.

#line

When we compile a program, if an error occurs, the compiler displays the name of the error file and the error in the first line of the file before the error.

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

#line number "filename"

Here is the number of new rows that will be assigned to the next line. The number of rows following it increments from this point.

FileName is an optional parameter that replaces the file name that is displayed when an error occurs after this line, until there is another #line instruction to replace it or until the end of the file. For example:

#line 1 "Assigning variable"
int a?;

This code will produce an error that appears in the file "assigning variable", line 1.

#error

This command interrupts the compilation process and returns an error message defined in a parameter, for example:

#ifndef __cplusplus
#error A C + + compiler is required
#endif

In this example, if __cplusplus is not defined, it interrupts the compilation process.

#include

We have seen this instruction many times. When the preprocessor finds an # include directive, it replaces the statement with the entire contents of the specified file. A declaration contains a file in two ways:

#include "file"
#include <file>

The only difference between the two expressions is what path the compiler should take to find the specified file. In the first case, the file name is written in double quotation marks, and the compiler first looks in the same directory as the file containing the instruction, and if the specified file is not found, the compiler looks for it under the configured default path (that is, under the standard header file path) .

If the file name is in angle brackets <>, the compiler looks directly under the default standard header file path.

Pre-defined identifiers

To handle some useful information, preprocessing defines some preprocessing identifiers, although the various compilers have different preprocessing identifiers, but they will handle the following 4 types:

__FILE__ the name of the file being compiled

__line__ the line number of the file being compiled

__date__ date string for compile time, for example: "Dec 2000"

__time__ time string for compile time, for example: "12:30:55"

For example:cout<< "The file is:" <<__file__ "<<"! The lines is: "<<__LINE__<<endl;

#pragma

This directive is used to configure the compiler, which is different for the platform and compiler you are using. For more information, please refer to your compiler manual.

Pre-C + + preprocessing directives

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.