A macro with parameters defines a format that resembles the following:
"#define Directive----macro with parameters" #define identifier (x1,x2,......, Xn)
Where x1,x2,...... xn is a marker (the parameter of the Macro)
Note: There is no space required between the name of the macro and the Parentheses.
If there are spaces, preprocessing will think of a simple macro defined, where (x1,x2,......, Xn) is part of the replacement list
When the preprocessor encounters a macro with parameters, the definition is stored for later Use. In later programs, if there is a macro call in identifier (y1,y2......,yn) format anywhere (where y1,y2, .... yn is a column tag), the preprocessor replaces it with a substitution list and uses Yi to replace the XI
e.g. if we define the following macro:
#define MAX (x, y) > (y)? (x): (y)) #define Is_even (n) ((n)%2==0)
The following example is a more complex macro:
#define TOUPPER (c) (' a ' <= (c) && (c) <= ' z '? (c)-' a ' + ' a ': (c))
A macro with parameters can contain an empty argument list, as Follows:
#define GETCHAR () getc (stdin)
Empty argument lists are not necessarily necessary, but can make getchar more like a function
Using a macro with parameters instead of the actual function has two advantages:
- The program may be slightly faster. A function call typically carries some extra overhead----store context information, Copy the value of a parameter, and so On. The call to a macro does not have these running costs
- Macros are more "generic". Unlike the parameters of a function, a macro parameter has no Type. therefore, macros can accept any type of argument as long as the preprocessed program is still VALID. E.g. we can use the Max macro to select the larger one from two numbers, the type of the number can be: Int,long int,float,double, etc.
- The compiled code will usually become larger. Each macro call results in a replacement list of the inserted macros, which in turn causes the program source code to increase (and hence the compiled code to be larger). The more frequently a macro is used, the more obvious this effect is. When macro calls are nested, the problem is superimposed on each other to make the program more Complex. e.g. N=max (i,max (j,k));
The following is the preprocessed statement:
n= ((i) > (((j) > (k)? ( j):(k))? (i):(((j) > (k)? J))) (:(k)));
- Macro parameters do not have type Checking.
- Cannot point to a macro with a pointer
- A macro may calculate its parameters more than Once. The function will only be evaluated once for its arguments, and the macro may be calculated two or more times. If the parameter has side effects, calculating the value of the parameter multiple times may produce unexpected results
Consider the following example, where one of max's parameters has side effects:
N=max (i++, J)
The following is the result of this statement after Preprocessing:
n= ((i++) > (j)? ( i++):(j));
Macros with parameters are not only suitable for calls to analog functions, they are often used as templates to handle the code snippets that we often want to write repeatedly:
e.g. we can use
#define Print_int (x) printf ("%d\n", x);
Make Print_int (x) Replace each use of printf ("%d\n", x);
Transfer from http://www.cnblogs.com/cpoint/p/3367386.html
Macro with parameters in C language