Macro with parameters in C Language
1. macros with Parameters
1. The macro definition with parameters is not a simple string replacement, but also a parameter replacement.
Definition Format:
# Define macro name (parameter table) String
1> string contains all the specified parameters in brackets
Eg: # define s (A, B) (a) * (B ))
Area = S (3, 2)
2> when the compilation system processes macro names with parametersProgramThe content in the brackets of the string specified in the row is processed from left to right. If a form parameter is encountered, it is replaced by a real parameter. If the non-form parameter is kept as is, the content after replacement is formed, during this period, no computation is performed.
Parameters in macro definition are called form parameters and parameters in macro calls are called real parameters.
3> when defining a macro, spaces should not be added between the macro name and the brackets with parameters; otherwise, content after spaces should be used as a part of the replacement string, that is, the system regards it as a macro definition without parameters.
2. Differences between macro definitions with parameters and functions
1> when a function is called, the value of the real parameter expression is obtained first, and then substituted into the form parameter. Parameters are replaced by simple characters (when the macro is expanded, values of parameter expressions are not realistic, and real parameters are replaced by parameters ).
2> function calls are processed when the program is running, and temporary memory units are allocated for the parameters. Macro expansion runs before compilation. During expansion, memory units are not allocated, direct transfer is not performed, and return values are not involved.
(In a function, "value transfer" is used, but in a macro with a parameter, it is replaced by a symbol, and there is no value transfer problem ).
3> both real parameters and form parameters in the function must define the type. The type requirements of the two parameters are the same. If they are different, type conversion is performed. Parameters in macros with parameters are of no type.
The macro does not have a type problem. The macro name does not have a type, and its parameters do not have a type. It is just a symbol. Just expand the character string specified by the times. When a macro is defined, a string can be any type of data.
Formal parameters in a macro with parameters do not need to be type defined, but real parameters in macro calls have specific values. Therefore, you must describe the type of real parameters.
4> when a large number of macros are used, the source program becomes longer after the macro is expanded, because every expansion will increase the program, and function calls will not make the source program become longer.
5> macro replacement does not occupy the running time, but the Compilation Time. Function calls consume the running time (allocating units, retaining the site, passing values, and returning values ).
3. Notes for using macros with parameters:
In macro definition, the form parameters in a string usually need to be enclosed in brackets to avoid errors,
And brackets should be added to the entire string.
eg: # define Q (x) (x) * (x)