C + + #define的用法 (with special)

Source: Internet
Author: User

1 NO parameter macro definition
The macro name of the parameterless macro is not followed by a parameter.
The general form of its definition is:
#define Identifier string
Where the "#" indicates that this is a preprocessing command. All that begin with "#" are pre-processing commands. "Define" defines the command for the macro. "Identifier" is the name of the macro defined. A "string" can be a constant, an expression, a format string, and so on.
The definition of a symbolic constant described earlier is an argument-free macro definition. In addition, macro definitions are often used for repeated expressions in the program.
For example:
#define M (Y*y+3*y)
Its function is to specify the identifier m to replace the expression (y*y+3*y). When writing the source program, all (Y*y+3*y) can be replaced by M, while the source program is compiled, the preprocessor will be a macro substitution, that is, the (y*y+3*y) expression to replace all the macro name M, and then compile.
Cases
#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);
}

In the above example, the macro is defined first, the M is substituted for the expression (y*y+3*y), and the macro is called in s=3*m+4*m+5* M. When preprocessed, the statement changes to a macro after it has been expanded:
s=3* (y*y+3*y) +4* (y*y+3*y) +5* (y*y+3*y);
Note, however, that there are no fewer brackets around the expression (y*y+3*y) in the macro definition. Otherwise, an error will occur. As defined below:
#difine M Y*y+3*y
The following statement is given when the macro expands:
S=3*y*y+3*y+4*y*y+3*y+5*y*y+3*y;
This is equivalent to:
3y2+3y+4y2+3y+5y2+3y;
Clearly inconsistent with the original test instructions requirements. The result of the calculation is of course wrong. Therefore, you must be very careful when you make a macro definition. You should ensure that no errors occur after a macro substitution.
For the macro definition, the following points are also stated:
1) macro definition is to use a macro name to represent a string, in the macro expansion and the string instead of the macro name, this is a simple substitution, the string can contain any character, can be a constant, or it can be an expression, the preprocessor does not make any checks on it. If there is an error, it can only be found when compiling a source program that has been expanded by the macro.
2) The macro definition is not a description or statement, there is no need to add a semicolon at the end, such as a semicolon is also replaced with a semicolon.
3) The macro definition must be written outside the function, and its scope is the macro definition command that ends with the source program. To terminate its scope, use the # undef command.
For example:
#define PI 3.14159
Main ()
{
......
}
#undef PI
F1 ()
{
......
}
Indicates that the pi is valid only in the main function and is not valid in F1.
4) If the macro name is enclosed in quotation marks in the source program, the preprocessor does not make a macro substitution for it.
Cases
#define OK 100
Main ()
{
printf ("OK");
printf ("\ n");
}

In the example above, the definition of the macro name OK represents 100, but the OK is enclosed in quotation marks in the printf statement, so no macro substitution is made. The running result of the program is: OK this means "OK" when the string processing.
5) macro definitions allow nesting, and you can use a macro name that has already been defined in a macro-defined string. is substituted by the preprocessor layer when the macro is expanded.
For example:
#define PI 3.1415926
#define S pi*y*y/* PI is a defined macro name */
Pair statement:
printf ("%f", S);
After the macro substitution becomes:
printf ("%f", 3.1415926*y*y);
6) The custom macro name is expressed in uppercase letters to make it easier to differentiate from variables. But it is also allowed in lowercase letters.
7) The use of macro definitions to represent data types makes writing easy.
For example:
#define STU struct STU
Stu can be used as a variable description in the program:
STU body[5],*p;
#define INTEGER INT
You can use integer as an integer variable description in your program:
INTEGER A, B;
It should be noted that the macro defines the difference between a data type and a typedef definition data specifier.
The macro definition is just a simple string substitution, which is done in preprocessing, and the TypeDef is processed at compile time, it is not a simple substitution, but a rename of the type descriptor. The named identifier has the function of the description of the type definition.
Take a look at the following example:
#define PIN1 int *
typedef (INT *) PIN2;
The two are similar in form, but they are not the same in actual use.
You can see the difference when you use pin1,pin2 to illustrate variables:
PIN1 A, B; After a macro substitution becomes:
int *a,b;
Indicates that a is a pointer variable that points to an integral type, and B is an integer variable.
However:
PIN2 A, B;
Indicates that a, B is a pointer variable that points to an integral type. Because PIN2 is a type descriptor. As this example shows, a macro definition can also represent a data type, but it is a character substitution. Be careful when you use it to avoid mistakes.
8) The "output format" as a macro definition, you can reduce the difficulty of writing.
This method is used in the example.
#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 (D f,a,b);
P (D f,c,d);
P (D f,e,f);
}

2 with parameter macro definition
The C + + language allows macros with parameters. The parameters in the macro definition are called formal parameters, and the parameters in the macro call are called actual parameters.
For a macro with parameters, in the call, not only the macro expansion, but also with the actual parameter to replace the parameter.
The general form of the parameter macro definition is:
#define Macro Name (formal parameter list) string
Contains the individual parameters in the string.
The general form of the call with a parameter macro is:
Macro name (argument table);
For example:
#define M (Y) y*y+3*y/* Macro definition */
......
K=m (5); /* Macro Call */
......
When the macro is called, the argument 5 is used instead of the formal parameter y, and the statement after the pre-processing macro expands:
K=5*5+3*5
Cases
#define MAX (A, B) (a>b)? a:b
Main () {
int X,y,max;
printf ("Input-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 a parameter macro, and the macro name Max represents the conditional expression (a>b)? a:b, formal parameters, a, a, are all present in the conditional expression. The seventh line of the program Max=max (x, y) is called for the macro, argument x, Y, and the substitution parameter, B. After the macro expands, the statement is:
    max= (x>y)? x:y;
Used to calculate the large number in X, Y.
The following questions are required for the macro definition with parameters:
1. In the parameter macro definition, there can be no spaces between the macro name and the formal parameter list.
   For example:
       #define MAX (A, B) (a>b)? A:b
Write as:
     #define MAX (A, B) (a>b)? A:B
is considered to be an parameterless macro definition, and the macro name Max represents a string (a, B) (a>b)? a:b. Macro Call statement when macro expands:
    max=max (x, y);
Will change to:
    max= (A, B) (a>b)? a:b (x, y);
This is obviously wrong.
2. In the parameter macro definition, the formal parameter does not allocate a memory unit, and therefore does not have to be a type definition. The arguments in the macro call have a specific value. To replace the parameters with them, you must make a type description. This is different from the case in the function. In the function, the formal parameter and the actual parameter are two different quantities, each has its own scope, when called to give the argument value to the formal parameter, carries on "the value passes". In the parameter macros, only the symbolic substitution, there is no problem of value passing.
3. The formal parameter in the macro definition is an identifier, and the argument in the macro invocation can be an expression.
Example
#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. The seventh line of the program macro call in the argument is a+1, is an expression, in the macro expansion, with a+1 substitution y, and then (y) * (y) substitution sq, the following statement:
sq= (a+1) * (a+1);
This is different from the call to the function, and the value of the argument expression is then given to the parameter when the function is called. In the macro substitution, the actual parameter expression is not calculated directly as the substitution.
4. In a macro definition, formal parameters within a string are usually enclosed in parentheses to avoid errors. In the macro definition in the example above (y) * (y) the expression y is enclosed in parentheses, so the result is correct. If the brackets are removed, the program is changed to the following form:
Cases
#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 of the operation is:
Input a Number:3
Sq=7

C + + #define的用法 (with special)

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.