The macro definition is one of the three preprocessing features provided by C, and these three preprocessing include: macro definition, file inclusion, conditional compilation1macro Definition without parameters: Macro definition is also called macro substitution, macro substitution, or macro. Format:#defineIdentifier stringThe identifier is called a symbolic constant, also known as a "macro name." Preprocessing (precompilation) work is also called macro expansion: Replace the macro name with a string. Master"Macro"The key to the concept is "change." Everything to change for the premise, do anything before the first change, accurate understanding before the "change." That is, before you make a specific analysis of the meaning and function of the relevant command or statement, you need to change it:#definePI 3.1415926Change the pi that appears in the program to 3.1415926Description: (1) The macro name is generally capitalized (2using macros can improve the versatility and readability of your program, reduce inconsistencies, reduce input errors, and facilitate modification. For example: array size common macro definitions (3preprocessing is the process of pre-compilation, and one of the tasks of compiling is grammar checking, and preprocessing does not do grammar checking. (4) No semicolon at the end of the macro definition;5The macro definition is written outside the curly braces of the function, and the scope is followed by the program, usually at the very beginning of the file. (6) You can use the #undef command to terminate the scope of the macro definition (7) macro definitions can be nested (8) string" "never contains a macro (9the macro definition does not allocate memory, and variable definitions allocate memory. 2. Macro definition with parameters: In addition to the general string substitution, the parameter substitution format is also done:#defineMacro name (parameter table) stringFor example:#defineS (A, b) a*b Area=s (3,2); The first step was changed to area=a*b; , the second step was changed to Area=3*2; Similar to a function call, there is a process of dummy binding: (1argument if it is an expression that is prone to problems#defineS (R) r*r Area=s (a+b); The first step is changed to Area=r*r; the second step is changed to area=a+b*a+C; The correct macro definition is#defineS (R) (r) * (R)(2the macro name and argument cannot have spaces between the parentheses (3macro substitution is only used for substitution, no calculation, no expression solving (4The function call occurs when the program runs after the compilation and allocates memory. Macro substitution is done before compiling, without allocating memory (5There is no type and no type conversion for the dummy combination of the macro. (6) function has only one return value, and with a macro you can try to get multiple values (7macro expansion causes the source program to grow longer, and the function call does not (8Macro expansion does not take up run time, accounting for compile time, function calls take up run time (allocate memory, hold field, Value Pass, return value)
Macro definitions without parameters and macro definitions with parameters