C ++ macro definition commands and macro definition commands

Source: Internet
Author: User

C ++ macro definition commands and macro definition commands
Commands starting with # In a program are pre-compiled commands. There are three types of pre-compiled commands: macro definition commands, file inclusion commands, and Conditional compilation commands. Today we will talk about macro definition: macro definition commands define an identifier as a string, the identifier in the source program is replaced by a specified string. Therefore, the pre-processing command usually has no extra points. This does not mean that all preprocessing commands cannot contain semicolons. The macro definition simply replaces a string with the macro name. Therefore, if a semicolon is added after the macro definition command, it will be replaced with the semicolon. In C ++, we generally use const to define constants. Obviously, defining constants with const is better than defining constants with define. The macro definition with parameters is generally in the following format: # define <macro name> (<parameter table>) <macro> where <macro name> is a identifier, <parameter table> can be one or multiple parameters, depending on the specific situation. When there are multiple parameters, each parameter is separated by a comma. <Macro> is a replacement string. the string in the macro is an identifier composed of parameters in the parameter table. For example: # define SUB (a, B) a-B if the following statement appears in the program: result = SUB (2, 3); it is replaced: result = 2-3; if the following statement is displayed in the program: result = SUB (x + 1, y + 2), it is replaced with: result = x + 1-y + 2; in this macro replacement process, the parameters in the parameter table are simply substituted into the macro expression. In the above example, a and B in the expression are substituted into 2 and 3 respectively. Note: (a) when writing the # define command, note that <macro name> and <string> are separated by spaces instead of equal signs. (B) The identifier defined by # define is not a variable. It is used only for macro replacement and therefore does not occupy the memory. (C) It is a habit to use upper-case letters to indicate <macro name>. The purpose of this Convention is to distinguish it from the variable name, because the variable name usually uses lower-case letters. If an identifier is defined as a macro name, it cannot be redefined before it is canceled. To cancel the macro definition, run the following command: # undef <identifier> where undef is the keyword. The function of this command is to cancel the <identifier> existing macro definition. The identifier of the macro definition is canceled and can be redefined. Macro definitions can be nested. defined identifiers can be used to define new identifiers. For example: # define PI 3.14159265 # define R 10 # define AREA (PI * R) Note: (1) the macro with parameters should be written in one row. If you need to write in multiple rows, end with the "\" extension when each row ends, except for the last row. (2) When writing a macro definition with parameters, no space is allowed between the <macro name> and the left parenthesis. Otherwise, the right part of the space is used as the macro. For example, # define ADD (x, y) x + y takes the whole of "(x, y) x + y" as the defined string. (3) when defining a macro with parameters, it is very important to properly add parentheses to the string with the same parameter name in the macro, so as to avoid possible errors. For example, for macro definition: # define SQ (x) x * x when the following statement appears in the program: m = SQ (a + B); replace the result: m = a + B * a + B; this may not be the expected result. If you need the following replacement result: m = (a + B) * (a + B ); the macro definition should be changed to: # define SQ (x) * (x). The method to expand and replace the macro definition with parameters is as follows: if a macro with real parameters (such as "SUB (2, 3)") exists 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) in the macro, the real parameters (such as constants, variables, or expressions) in the Program Statement are replaced by the form parameters, if the character in the macro definition is not a parameter character (such as the-number in a-B), it is retained. This forms a replacement string. (4) When defining macros with parameters, it is best to avoid passing parameters using expressions. In this way, problems in (3) can be avoided in complicated macro definitions.

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.