Macro definition of C Language

Source: Internet
Author: User

There are two types of macro definitions: macro definitions without parameters and macro definitions with parameters.

Macro definition without Parameters
The general format of macro definition without parameters is:

# Define macro name string
# Define PI 3.1415926
Main ()
{
Float area, R;
Printf ("Area = % F", pI * r * R );
}
In the above program, the macro pi is used to replace the string 3.1415926. The advantage of doing so is obvious. One is to simplify the program, and the other is to facilitate modification.
Note:
1. macro definitions are generally written at the beginning of a program.
2. The naming rules for macro names are the same as those for variable names. Generally, uppercase letters such as Pi are used to distinguish variables in programs.
3. The valid range of macro names is from macro definition to source program file, or when preprocessing command # UNDEF is run.
# Define PI 3.14
Valid range of macro pi
Main ()
{
...
}
# UNDEF pi
F1 ()
{
..
}
4. macro definition can not only define constants, but also define C statements and expressions. Macro definitions can also be nested. That is, the string in the macro definition
To reference the macro name defined by another macro definition. # Define PI 3.14
# Define R 30
# Define area pI * r * R
# Define PRN printf ("/N ");
Main ()
{
Printf ("% lf", area );
Prn
}
5. Macro replacement is a simple replacement of the specified string without any syntax check. It is only possible to report an error when compiling the expanded source program. As follows:
The semicolon is replaced with the semicolon. The fourth sentence in the preceding example.
6. You cannot replace the string with the same macro name in double quotation marks or the element with the same macro name in the User Identifier. Such as PI in printf ("pi"), and PIR
The PI of is the same as the macro name previously defined, but they cannot be replaced by 3.14.
Macro definition with Parameters
The macro definition with parameters is generally in the following format:
# Define macro name (parameter table) String
For example, # define M (a, B) a * B
...
S = m (3, 5 );
Note:
1. In the preceding macro call, the result is S = 3*5 = 15. That is, the actual parameters 3 and 5 are used to replace the form parameters A and B. The number of real parameters must be the same as the number of form parameters without the type requirement,
Different from function calls, function calls require the same parameter type.
2. if the macro call is changed to S = m (3 + + 1), the call is S = 3 + 2*5 + 1, which is obviously inconsistent with the expected result. In this case, the macro definition should be changed
# Define M (a, B) (a) * (B)
In this way, we can get the expected result s = (3 + 2) * (5 + 1 ).
3. If the macro call is changed to S = 3/M (3 + + 1), replace S = 3/(3 + 2) * (5 + 1)
The expected results do not match. In this case, the macro definition should be changed
# Define M (A, B) (a) * (B ))
In this way, we can get the expected result s = 3/(3 + 2) * (5 + 1 )). The above two points are used to draw attention to the use of parentheses in macro definition.
4. If the macro definition contains a String constant enclosed by double quotation marks and contains a form parameter, the real parameter cannot be replaced with the form parameter in the double quotation marks during macro replacement. For example:
# Define add (m) printf ("M = % d/N", m)
Use the add (x + y) statement. The result is printf ("M = % d/N", X + Y );
To solve this problem, you can add a "#" before the parameter to the form below:
# Define add (m) printf (# M "= % d/N", m)
The add (x + y) Statement is called, and the result is changed to printf ("x + y = % d/N", X + Y );
5. If the macro definition contains the double "#", remove the double "#" when replacing the macro and combine the front and back strings. For example:
# Define s (a, B) a # B
When the S (define, 5); statement is called, the macro is define5.

 

 

 

 

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.