Macros in the C language

Source: Internet
Author: User

1. Macro Definitions without parameters:
A macro definition is also called a macro substitution, a macro substitution, or a macro.
Format:
#define Identifier string
The identifier is called a symbolic constant, also known as a "macro name."
Preprocessing (precompilation) work is also called macro expansion: Replace the macro name with a string.
The key to mastering the concept of "macro" is "change". Everything to change for the premise, do anything before the first change, accurate understanding before the "change."
That is, before you make a specific analysis of the meaning and function of the relevant command or statement, change:
Cases:
#define PI 3.1415926
Change the pi that appears in the program to 3.1415926.
Description
(1) Macro names are generally capitalized
(2) The use of macros can improve the universality and readability of the program, reduce inconsistencies, reduce input errors and facilitate modification. Example: array size common macro definitions
(3) Preprocessing is the processing before compiling, and one of the tasks of compiling is grammar checking, preprocessing does not do grammar checking.
(4) No semicolon at the end of the macro definition;
(5) The macro definition is written outside the curly braces of the function, and the scope is followed by the program, usually at the very beginning of the file.
(6) The scope of the macro definition can be terminated with the #undef command
(7) macro definition can be nested
(8) The string "" never contains a macro
(9) Macro definition does not allocate memory, variable definition allocates memory.
2. Macros with parameters:
In addition to the general string substitution, the parameter substitution
Format:
#define Macro Name (parameter table) string
Example: #define S (A, b) a*b
Area=s (3,2); The first step was changed to area=a*b; , the second step was changed to area=3*2;
Similar to a function call, there is a process of dummy binding:
(1) argument if it is an expression prone to problems
#define S (R) r*r
Area=s (a+b); The first step is changed to Area=r*r; the second step is changed to area=a+b*a+b;
The correct macro definition is the # define S (R) (r) * (R)
(2) There must be no spaces between the macro name and the argument's parentheses
(3) macro substitution only for substitution, no calculation, no expression solution
(4) The function call occurs when the program runs and allocates memory. Macro substitution is done before compilation, memory is not allocated
(5) There is no type and no type conversion for the dummy combination of the macro.
(6) function has only one return value, using a macro can try to get multiple values
(7) Macro expansion causes the source program to grow longer, and function calls do not
(8) Macro expansion does not occupy the running time, only the compilation time, function calls take up the run time (allocate memory, preserve field, value Pass, return value)

Macros in the C language

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.