Define usage and define usage

Source: Internet
Author: User

Define usage and define usage

Define usage Summary

The use of define is only a pure replacement function, and the replacement of macro definition is the replacement of pre-processor processing.

I. Simple macro definition usage

Format: # define identifier replacement content

The content to be replaced can be numbers, characters, strings, special characters, and spaces. The content to be followed will be replaced with anything.

For example:

# Define N 5 is equivalent to int array [5];

Int array [N];

Same effect:

# Define N = 5

Int array [N]; equivalent to int array [= 5];

Same effect:

# Define N 5;

Int array [N]; equivalent to int array [5;];

A common error:

# Define pin int *

Pin a, B; the actual effect is int * a, B;

  

# Define N 2 + 2

Void main (void)

{

Int a = N * N;

Printf ("% d \ n", );

}

The result is 2 + 2*2 + 2 = 8.

 

Ii. Use of macro definitions with Parameters

Example: A function for Square area

# The correct define statement should be:

# Include <stdio. h>
# Define area (x) * (x ))
Int main (void)
{
Int s = area (3 + 3 );
Printf ("s = % d \ n", s );
Return 0;
}

Running result: (3 + 3) * (3 + 3) = 36 is the expected result.

FAQs:

# Include <stdio. h>
# Define area (x) x * x
Int main (void)
{
Int s = area (3 + 3 );
Printf ("s = % d \ n", s );
Return 0;
}

Running result: 3 + 3*3 + 3 = 15 is not the expected result.

This better shows that the macro definition of define mentioned above is a pure replacement, which is done after the replacement.

Methods to prevent this problem:

To make good use of the macro definition and prevent errors in the two common situations above, remember to replace all the macro usage in the program with the string it represents in the thinking, do not add any other symbols on your own. Do not write the wrong running result after the calculation is fully expanded. When macro replacement is used for programming, when there are more than one symbol in the string, brackets indicate the priority. If it is a macro definition with parameters, each parameter in the macro must be added with parentheses, add a bracket to the entire macro.

 

Iii. encapsulation of functions

Example: Now the mult function is used to calculate the product of two numbers.

[Linux @ centos-64-min exercise] $ cat mul. c
# Include <stdio. h>
Int mult (int x, int y)
{
Int result = x * y;
Return result;
}

Now we need two different function functions. One is the square_area function for Square area and the other is the rectangle_area function for rectangular area.

You can write as follows:

[Linux @ centos-64-min exercise] $ cat mul. c
# Include <stdio. h>
Int mult (int x, int y)
{
Int result = x * y;
Return result;
}
[Linux @ centos-64-min exercise] $ cat try. c
# Include <stdio. h>
Int mult (int x, int y);/* Declaration of the function for multiplying two numbers */

# Define square_area (str, x) mult (x, x)/* encapsulate the function into a square area */
# Define rectangle_area (str, x, y) mult (x, y)/* encapsulate it into a function for rectangular area */

/* The above function declaration and macro definition should be written in a standard way and should have been put in a header file. Here, the problem is simply put in the function */
Int main (void)
{
Int s = 0;
S = square_area ("This is the area of the square", 3 );
Printf ("This is the area of the square: s = % d \ n", s );
S = rectangle_area ("This is the area of the rectangle", 3, 4 );
Printf ("This is the area of the rectangle: s = % d \ n", s );
Return 0;
}

Running result:

[Linux @ centos-64-min exercise] $ gcc-o try. c mul. o
[Linux @ centos-64-min exercise] $./try
This is the area of the square: s = 9
This is the area of the rectangle: s = 12

 

4. Three special symbols in define :#,##,#@

# Define Conn (x, y) x # y
# Define ToChar (x) # @ x
# Define ToString (x) # x

X # y indicates that x connects to y, for example:
Int n = Conn (123,456); the result is n = 123456;
Char * str = Conn ("asdf", "adf") returns str = "asdfadf ";

# @ X, in fact, is to add single quotes to x, and the returned result is a const char. For example:
Char a = ToChar (1); the result is a = '1 ';
Make an out-of-bounds test char a = ToChar (123); the result is wrong;
However, if your parameter contains more than four characters, the compiler reports an error! Error C2015: too character characters in constant: P

# X, which indicates adding double quotation marks to x
Char * str = ToString (123132); then str = "123132 ";

 

V. Summary # define macro definition

(1) convenient program modification
Use a simple macro definition to replace a constant that is frequently used in the program. In this way, when changing the constant, you do not need to modify the entire program, but only modify the macro-defined string, and when the constant is relatively long, we can use a short meaningful identifier to write the program, which is more convenient.

(2) macro definition is replaced during pre-compilation. After the sub-function is called in the program, the sub-function must be called after meals to continue the execution. This will cause the consumption of Function Conversion. However, this problem does not occur when the macro definition with parameters is used, because the macro is expanded in the pre-processing stage and no conversion is required during execution, that is, the macro definition is executed locally, however, complex operations must be performed by function calls, and the macro definition occupies a large space for the target code. Therefore, you must determine whether to use macro definitions based on specific conditions.

 

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.