C Language Learning Tutorial Nineth chapter-pretreatment (2)

Source: Internet
Author: User
Tags printf

With the parameter macro definition

The C language allows macros with parameters. A parameter in a macro definition is called a formal argument, and a parameter in a macro invocation is called an actual argument. For macros with parameters, in the call, not only macro expansion, but also the use of arguments to replace the formal parameters.

The general form of a parameter macro definition is: #define Macro name (formal parameter list) string contains individual parameters in the string. The general form of the call with the parameter macro is: the macro name (the actual parameter list);
For example:
#define M (Y) y*y+3*y/* Macro definition */
:
K=m (5); /* Macro Call * *
: When a macro is invoked, the argument 5 is substituted for the parameter Y, and the statement after the preprocessing macro expands
As: k=5*5+3*5
#define MAX (A,b) (a>b)? a:b
Main () {
int X,y,max;
printf ("Input two numbers:");
scanf ("%d%d", &x,&y);
Max=max (X,y);
printf ("max=%d\n", Max);
}
The first line of the previous example program is defined with the parameter macro, and the macro name Max represents the conditional expression (a>b) a:b, and the formal parameter a,b appears in the conditional expression. Program line Seventh Max=max (x,
Y) is called for a macro, the argument x,y, the substitution of the formal parameter a,b. When the macro expands, the statement is: max= (x>y)? X:y, which is used to calculate the large number in x,y. The following issues need to be explained for macro definitions with parameters:

1. There is no space between the macro name and the formal parameter list in the definition of a parameter macro.
For example: #define MAX (A,B) (a>b) a:b as: #define MAX (A,B) (a>b)? A:b is considered a parameterless macro definition, and the macro name Max represents a string (a,b) (a>b)? a:b.
When a macro expands, the macro invokes the statement: Max=max (x,y), which becomes: max= (a,b) (a>b) a:b (x,y); This is clearly wrong.

2. In the definition of a parameter macro, a formal parameter does not allocate a memory cell and therefore does not have to be a type definition. The arguments in the macro call have specific values. To use them to replace the formal parameters, you must do a type description. This is different from the situation in the function. In a function, a formal parameter and an argument are two different quantities, each with its own scope, and when called, the argument value is given to the formal parameter for "value delivery." In the parametric macro, it is only symbolic substitution, and there is no problem of value transfer.

3. A formal parameter in a macro definition is an identifier, and an argument in a macro invocation can be an expression.
#define SQ (y) (y) * (y)
Main () {
int a,sq;
printf ("Input a number:");
scanf ("%d", &a);
SQ=SQ (a+1);
printf ("sq=%d\n", sq);
}
In the example above, the first behavior macro is defined, and the formal parameter is Y. In the seventh line of the program, the actual parameter in the macro call is A+1, which is an expression, when the macro expands, use the a+1 substitution y, and then replace the sq with (y) * (y) to get the following statement: sq= (a+1) * (a+1); This is different from the call to the function, when the function is called to find the value of the argument expression and then give the formal parameter. In the macro substitution, the actual parameter expression is not calculated directly as the substitution.

4. In a macro definition, the formal parameters within a string are usually enclosed in parentheses to avoid errors. Y in the macro definition (y) * (y) expression in the example above is enclosed in parentheses, so the result is correct. If you remove the parentheses, change the program to the following form:
#define SQ (y) y*y
Main () {
int a,sq;
printf ("Input a number:");
scanf ("%d", &a);
Sq=sq (a+1);
printf ("sq=%d\n", sq);
}
The result is: input a number:3
Sq=7 also enter 3, but the result is not the same. What's the problem? This is due to substitution only for symbolic substitution and not for other treatment. Macro substitution will get the following statement: sq=a+1*a+1; Since a is 3 sq, the value is 7. This obviously does not match the argument, so the parentheses on either side of the parameter are not small. Even if parentheses are not enough on either side of the argument, see the following program:
#define SQ (y) (y) * (y)
Main () {
int a,sq;
printf ("Input a number:");
scanf ("%d", &a);
Sq=160/sq (a+1);
printf ("sq=%d\n", sq);
}
This program only changes the macro call statement to: SQ=160/SQ (a+1), and if the input value is still 3 o'clock, expect the result to be 10. However, the results of the actual operation are as follows: Input a number:3 sq=160 why would this result be? Parse the macro invocation statement, which changes after the macro substitution: sq=160/(a+1) * (a+1); A is 3 o'clock, because the "/" and "*" operators have the same precedence and binding, The first 160/(3+1) is 40, then 40* (3+1) the last 160. In order to get the correct answer the entire string in the macro definition, plus parentheses, the program modifies the following
#define SQ (y) (y) * (y))
Main () {
int a,sq;
printf ("Input a number:");
scanf ("%d", &a);
Sq=160/sq (a+1);
printf ("sq=%d\n", sq);
}
For the above discussion, the macro definition should be not only parentheses on both sides of the argument, but also the entire string plus parentheses.

5. A macro with a parameter is very similar to a parameter function, but there are essentially differences, except for the points mentioned above, the result of the same expression being treated with a function and using a macro can be different. Main () {
int i=1;
while (i<=5)
printf ("%d\n", SQ (i++));
}
SQ (int y)
{
return ((y) * (y));
} #define SQ (y) (y) * (y))
Main () {
int i=1;
while (i<=5)
printf ("%d\n", SQ (i++));
}
In the example above, the function name is sq, the formal parameter is Y, and the function body expression is ((y) * (y)). In example 9.6, the macro name is sq and the formal parameter is Y, and the string expression is (y) * (y)). The two cases are the same. The function call for example 9.6 is sq (i++), and the macro call for example 9.7 is sq (i++), and the arguments are the same. From the output results, but very different. The analysis is as follows: in example 9.6, a function call increases the argument I value by 1 after it is passed to the formal parameter Y. The function value is then output. So you have to circulate it 5 times. The square value of the output 1~5. In example 9.7, when the macro is invoked, only substitution is made. The SQ (i++) is substituted ((i++) * (i++)). In the first round, since I equals 1, the calculation process is as follows: The first I initial value in the expression is 1, then I from 1 to 2, so the 2nd I initial value in the expression is 2, and the result of the two-phase multiplication is 2, then I increment 1 by 3. In the second cycle, I values already have an initial value of 3, so the first I in the expression is 3, the latter I is 4, the product is 12, then I increment 1 into 5. Into the third loop, because I is already 5, so this will be the last loop. The value of the evaluated expression is 5*6 equal to 30. I value from 1 to 6, no longer meet the cycle conditions, stop the loop. From the above analysis you can see that function calls and macro calls are similar in form, in essence, is completely different.

6. Macro definitions can also be used to define multiple statements, which are then substituted into the source program when the macro is invoked. Look at the example below.
#define SSSV (s1,s2,s3,v) s1=l*w;s2=l*h;s3=w*h;v=w*l*h;
Main () {
int L=3,W=4,H=5,SA,SB,SC,VV;
SSSV (SA,SB,SC,VV);
printf ("sa=%d\nsb=%d\nsc=%d\nvv=%d\n", SA,SB,SC,VV);
}
Program first behavior macro definition, with macro name SSSV 4 assignment statements, 4 parameters are 4 assigns the left part of the variable. When a macro is called, expand the 4 statements and replace the formal parameters with arguments. Causes the results of the calculation to be fed into the argument.

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.