# Define usage

Source: Internet
Author: User

# DefineCreate a macro that is an identifier or parameterized identifier associated with the tag string. After defining the macro, the compiler can mark a string to replace each matching item of the identifier in the source file.

Syntax

# Define Identifier Token-string 

# Define Identifier ( Identifier1, ... , IdentifierN) Token-string 

Remarks

1 # defineCommand to enable the CompilerToken-stringReplacing the source fileIdentifier. Only whenIdentifierIt is replaced only when a tag is formed. That is to say, ifIdentifierIf it appears in a comment, string, or long identifier, It is not replaced.

2. The second form is similar to a function, for example

# The define MUL (a, B) (a * B) parameter name will appear inToken-stringTo mark the replacement location of the actual value.

Token-stringWithout stringizing (#), Charizing (#@) Or token-pasting (##) Operator or not followed##Each parameter of the operator is replaced by the corresponding real parameter.

 

Note:

1 macro is a simple replacement of characters. Note that the priority of the replaced operator may affect the expression of the macro definition. So be good at using parentheses. For example:

# Define add (A, B) A + B if it is used: 2 * Add (1, 2) originally wanted to make it equal to 6, but it is changed to 2*1 + 2 = 4 after replacement. So pay attention to it.

2 # The Line Break In define is a backslash \. If a row is not written, add a \ directly. You can continue to write the next row.

3. Note that this is only a replacement of characters. The macro definition should not contain return or other statements. Think about what it will look like after replacement.

 

Two formats of macro return values:

1 operator.

# Define max (A, B) (a)> (B) (a) :( B ))

# Define add (A, B) (a) + (B ))

2. Assign the return value to an additional parameter, which is the return value.

# Define CAL (a, B, c) {c = a + B ;}

 

An interesting example:

Defines a macro for maximum value.

It may be written as follows: # define max (A, B) (a)> (B )? A) (B ))

This may not be wrong in most cases, but it is not rigorous. For example:

# Define max (A, B) (a)> (B )? (A) (B ))

Int fun (int *)

{

* A = * A + 1;

Return *;

}

Main ()

{

Int A = 1, B = 2, C;

C = max (A, fun (& B ));

} // The value of C we want should be 3, but the actual value of C is 4.

Because first we replace: c = (a)> (fun (& B ))? (A) :( fun (& B )));

We can find that fun is executed twice, and the value of B is added twice to 4. Therefore, the result is 4.

 

Here is a rigorous practice:

So that the parameter is executed only once.

# Define max (A, B )({\

Typeof (a) _ A = ();\

Typeof (B) _ B = (B );\

(_ A> _ B )? _ X: _ y ;})

In this way, the above problems will not occur.

Note :({...}) the function is to return the last value of several internal statements. It also allows variable declaration in the internal (because it forms a local scope through braces ). This can achieve the purpose of return.

!! Note: typeof is used here, which is a new extension of C. However, it cannot be used in vs2005. It can only be executed under the online compiler and the result is correct.

We can see that the keyword typeof is not recognized in. In C ++, decltype can be used to implement similar functions and evaluate the type of a variable. In addition, vs cannot have such. Either # define... () or # define ..{}.

So the above method can be changed:

# Define min (X, Y, m ){\

Decltype (x) X _ = (x );\

Decltype (y) Y _ = (y );\

M = (X _ <Y _)? X _: Y _;}

Use Time: int A = 1, B = 2, C; max (A, B, C); C is the return value, not involved in the operation.

 

 

######@ Usage in # define:

 

# It means not to expand the Parameter

IfToken-stringIt starts with #, meaning that the parameter is directly replaced without being expanded.

IfToken-stringIs not starting with #, which means to expand the parameter and replace it directly.

For example:

# Define F (a) A + 1

# Define T (a) #

# Define H (a)

T (f (a)-> F (a)-> A + 1

H (f (a)-> H (A + 1)-> A + 1

Although the results are the same, the process is different.

 

# It means a string Connector

For example:

# Define f (a, B) a ## B ## lobe

The result is ablobe.

Note: ## it cannot start or end. It can only appear in the middle.

 

# @ Characters' Operators

For Microsoft

The charizing operator can only be used with macro parameters. If there are#@When the macro is extended, real parameters are enclosed in single quotes and treated as one character.

#define makechar(x)  #@x

=

Char A = makechar (X );

For example:

# Define h (x) # @ x

Cout <H (a) <Endl;

Output result:

Cout <H (AB) <Endl;

Output result: 24930

Annotation: It is equivalent to converting the real parameter after # @ into a character of the character type.

# Define usage

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.