From

xiaoxiao2021-03-06  75

Word Source VC World - C language classroom

Self-archiving, also dedicated to the same Delphi programmer as me

Macro Definition Allows a string in the C language source program to represent a string called "macro". The identifier defined as "macro" is called "macro name". When compiling pretreatment, all the "macro names" that appear in the program are replaced by the string in the macro definition, which is called "macro replacement" or "macro".

Macro definition is done by the macro definition command in the source program. The macro change is automated by the pre-processing program. In the C language, "macro" is divided into parameters and no parameters.

No argument without parameters after unfolding macro. The general form of its definition is: #define identifier string "#" indicates that this is a preprocessing command. Everyone starts with "#" is the pretreatment command. "Define" is a macro definition command. "Identifier" is the macro name defined. "String" can be constant, expressions, formatters, etc. The definition of symbol constant described above is a no-array-free definition. In addition, the expression that repeatedly used in the program is often macro definition. #define m (Y * Y 3 * Y) Main () {Int S, Y; Printf ("INPUT A Number:"); Scanf ("% D", & Y); s = 3 * m 4 * m 5 * m; Printf ("S =% D / N", S);} Macro definition in the above case, define M expression (Y * Y 3 * Y), in s = 3 * m 4 * m 5 * m make a macro. After pretreatment, the statement becomes: s = 3 * (Y * Y 3 * Y) 4 (Y * Y 3 * Y) 5 (Y * Y 3 * Y); It should be noted that the brackets in the macro definition (Y * Y 3 * Y) cannot be less. Otherwise an error will occur. After the following definition: #difine M Y * Y 3 * Y will get the following statement: s = 3 * y * y 3 * Y 4 * Y * Y 3 * Y 5 * Y * Y 3 * Y; this is equivalent to; 3Y2 3y 4Y2 3y 5y2 3y; obviously does not match the original question. The calculation result is of course wrong. Therefore, you must pay attention when making a macro definition. There should be no errors after the macro change. For macro definitions, the following points are also described:

1. Macro definition is to indicate a string with a macro name, replacing the macro name at the macro show, which is just a simple substitution, and the string can contain any characters, which can be constant, or It is an expression that the pre-processing program does not check it. If there is an error, it can only be found when compiling the source program that has been opened by the macro.

2. Macro definition is not an explanation or statement, do not have a semicolon at the end of the row, such as plus semicolons, even replacement together.

3. Macro definition must be written outside of the function, and its scope functions as the macro definition command to the source program end. To terminate its scope, the # undef command can be used, for example: # define pi 3.14159main () {...} # UNDEF PIPI's scope F1 () ... indicating that PI is only valid in the main function, in F1 invalid. 4. Macro name In the source program, if the quotation is enclosed, the pre-processing program does not make a macro change. #DEfine OK 100mAin () {Printf ("OK"); Printf ("/ n");} The macro name OK represents 100 in the previous example, but in the printf statement, the OK is included, so it does not make a macro change. The result of the program is: OK This means that "OK" is handled as a string.

5. Macro definition allows nested, which can be used in the macro defined string. Switched by the pretreatment program layer by the macro show. For example: #define pi 3.1415926 # Define S PI * Y * Y / * PI is a defined macro name * / 对 statement: Printf ("% f", s); in the macro change to: Printf ("% f ", 3.1415926 * Y * Y); 6. The habitual macro name is expressed in uppercase letters in order to distinguish between variables. However, lowercase letters are also allowed.

7. The macro definition indicates the data type and makes the writing easily. For example: #define STU STRUCT STU Available in the program: STU Body [5], * P; #define integer int in the program to use Integer to make an integer variable description: Integer A, B; should pay attention to use macro Define the difference between the data type and define the data specifier with typedef. Macro definition is just a simple character string replacement, which is done in preprocessing, and TypeDef is processed at compile, it is not a simple substitution, but rename the type specifier. The named identifier has the function of type definition description. Please see the example below: #DEfine Pin1 INT * TYPEDEF (int *) PIN2; seeing these two in the form, but in actual use. When the variable is used in PIN1, PIN2 can be seen when the variables can be seen: PIN1 A, B; turn into int * a, b after macro generation; representing A means a pointer variable to the integer, and B is integer variable . : PIN2 A, B; represents A, B are all pointer variables to integer. Because Pin2 is a type of specifier. As can be seen from this example, although the macro definition can also represent the data type, it is made by the character. Be careful in use to avoid errors.

8. Make the "Output Format" macro definition, you can reduce writing trouble. This method is used in Example 9.3. #define p printf # define D "% D / N" #define f "% f / n" main () {int a = 5, c = 8, E = 11; float b = 3.8, D = 9.7, f = 21.08; P (df, a, b); p (df, c, d); p (df, e, f);}

With macro

C language allows macros with parameters. The parameters in the macro definition are called the formal parameters, and the parameters in the macro call are called actual parameters. Macro with parameters, in the call, not only the macro is displayed, but also use the active parameters to change the shape.

A general form of a macro definition is: #define macro name (Distake) string contains individual ginseng in the string. The general form of the ginseng macro call is: macro name (real parameter table); for example: #define m (y) y * y 3 * Y / * macro definition * /: k = m (5); / * macro * /: When the macro is called, replacing the argument y, the statement after the pre-processed macro departure is: k = 5 * 5 3 * 5 # define max (a, b) (A> B) ? A: bmain () {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 above-case program is defined by the envelope macro, indicating the conditional expression (A> b) with a macro name MAX? A: B, the meticulum a, b appears In the conditional expression. The seventh rows of the program MAX = max (x, y) are macro modes, universal parameters x, y, will be changed to the shapes A, B. After the macro show this statement is: max = (x> y) x: y; used to calculate the large number of x, y. There is a problem with the macro definition of the parameter. For example, #define max (a, b) (a> b)? A: B is written as: #define max (a, b) (A> b)? A: B will be considered a macro definition, macro Name Max represents strings (A, B) (a> b)? A: B. Macro show, macro call statement: max = max (x, y); becomes: max = (a, b) (a> b)? A: B (x, y); this is obviously wrong.

2. In the belt macro definition, the formal parameters do not assign the memory cell, so it is not necessary to define the type. In the macro, the real value is specifically valued. To use them to change the resort, therefore must be described. This is different from the situation in the function. In the function, the meticulum and the argument are two different amounts, each has their own scope, and the real gate value is given to the intersection, and "value delivery" is performed. And in the belt macro, it is just a symbolic change, there is no problem that the value is transmitted.

3. The shape parameter is an identifier in the macro definition, and the real parameters in the macro call can be an expression. #define SQ (Y) * (Y) Main () {Int A, Sq; Printf ("Input A Number:"); Scanf ("% D", & A); SQ = SQ (A 1) PRINTF ("SQ =% D / N", SQ);} The first behavior macro definition in the previous example, the shape is Y. The seventh row macro modulus is a 1, which is an expression. When the macro shows, use A 1 to change Y, then use (Y) * (y) to change SQ, get the following statement: sq = (A 1) * (A 1); this is different from the call to the function, and the value of the real parameter expression is given to the ginseng when the function is called. In the macro changing, the real-argument expressions are not calculated directly to change.

4. In the macro definition, the guanmens within the string usually enclose with parenthesis to avoid errors. In the macro definition in the above example, Y, * (Y) expression is enclosed in parentheses, so the result is correct. If you go to the bracket, change the program to the following form: #define SQ (Y) Y * Ymain () {Int A, SQ; Printf ("Input A Number:"); Scanf ("% D", & A); SQ = SQ (A 1); Printf ("SQ =% D / N", SQ);} The run result is: Input a number: 3sq = 7 is also input 3, but the result is different. Where is the problem? This is due to the generation of only the symbols and do not do other processing. The following statement will be obtained after the macro change: SQ = a 1 * a 1; since A is 3, the value of SQ is 7. This is clearly related to the meaning of the title, so parentheses in both sides of the parameter cannot be less. Even if the argument is bracker, it is still not enough, please see the following procedures: #define SQ (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 is changed to: sq = 160 / Sq (A 1); Run this program If the input value is still 3, the desired result is 10. However, the actual operation is as follows: Input a number: 3 SQ = 160 Why is this result? Analyze macro modulus statement, change to: SQ = 160 / (1) * (A 1) after macro change ); when A is 3, since "/" and "*" operator priority and combination are the same, it is 160 / (3 1) to obtain 40, and then 40 * (3 1) finally has 160. In order to get the correct answer, the entire string should be scaled in the macro definition, and the program modifies the following #define sq (y) ((y)) main () {Int A, SQ; Printf ("Input A Number: "); scanf ("% d ", & a); sq = 160 / sq (A 1); Printf (" SQ =% D / N ", SQ);} The above discussion description, not only in parameters Both sides are added parentheses, should also be taken from parentheses throughout the string.

5. The macro with the belt is very similar, but there is an essential difference, in addition to the above, the result of function processing with the same expression and the use of macros may be different. Main () {INT i = 1; While (i <= 5) Printf ("% D / N", SQ (i ));} SQ (int y) {return ((y) * (y));} #define sq (y) ((y)) main () {INT i = 1; while (i <= 5) Printf ("% d / n", SQ (i ));} In the example The function name is SQ, the shape is Y, the function body expression is ((y) * (y)). In Example 9.6 macular macro names SQ, the metallin is also Y, the string expression is (Y) * (Y)). Two cases are the same. Example 9.6 The function call is SQ (i ), the macro modulation of Example 9.7 is SQ (i ), and the active parameters are also the same. From the output result, it is very different. The analysis is as follows: In Example 9.6, the function call is to increase the solid parameter I value from Y. Then output the function value. Therefore, it is necessary to circulate 5 times. Output 1 to 5 square values. In Example 9.7, the macro is called only to change. SQ (i ) is replaced ((i ) * (i )). In the first cycle, since i is equal to 1, the calculation process is: the previous value of the previous I is 1, and then I has increased from 1 to 2, so the second I initial value in the expression is 2, The results of the two multiplication are also 2, and then the I value increases from 1 to 3. In the second cycle, the I value has an initial value of 3, so the previous I is 3, and the latter I is 4, and the product is 12, and then I will increase from 1 to 5. Enter the third cycle, since the I value has been 5, this will be the last cycle. The value of the calculation expression is 5 * 6 equal to 30. The I value has increased from 1 to 6, no longer satisfying the cycle condition, stopping the cycle. From the above analysis, it can be seen that both of the function calls and macro modes are similar in form and is essentially completely different. 6. Macro definition can also be used to define multiple statements. When macro calls, the statements are replaced to the source program. 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);} The first behavior macro definition is used to represent the four assignment statements with macro name SSSV, and the four type gauges are four variables on the left of the value. When the macro is called, 4 statements are expanded and replaced with the active parameters. Make the calculation results into the argument.

转载请注明原文地址:https://www.9cbs.com/read-120610.html

New Post(0)