Recall: # define usage,

Source: Internet
Author: User

Recall: # define usage,

Ansi c stipulates that: # can contain spaces or tabs, and # can contain spaces between the command and other parts. It can appear anywhere and the scope ranges from the definition to the end of the file.

Before preprocessing, the system deletes the combination of backslash and linefeed. Therefore, commands can be extended to several physical rows, which form a single logical row.

// Each # define line (logical line): consists of three parts // macro replacement list (or subject) of the instruction itself # define PI 3.141592653

Macros are classified into Class Object macros (macro representing values) and class function macros.
The macro name cannot contain spaces. It must follow the c naming rules. The final replacement text from the macro to the macro is called macro expansion. The pre-processor does not perform computation, but simply replaces the text.

String of the language symbol type and string of the character type

The system regards the subject as a string of the language symbol type, rather than a string of the character type. The language symbol in the Preprocessor is a separate word in the macro-defined subject, which is separated by blank characters.

# Define SIX 2*2 // The definition contains a language symbol. The sequence 2*2 # define AAA 2*2 // The definition contains three language symbols: 2, *, 3

If the subject is interpreted as a character string, the pre-processor replaces AAA with 2X2, and the extra space is replaced.

If the subject is interpreted as a string of the language symbol type, the Preprocessor replaces AAA with only three language symbols separated by a single blank character.

The spaces in 2*2 are only the symbols used to separate the symbols of the subject language. They are not counted (Note: Different compilers use different methods)

Redefinition of constants

I started to define AAA as constant 4. Later in this file, I defined AAA as 10, which is called a redefinition constant. Different compiler policies are different,However, standard c stipulates that this is incorrect. Only New and Old definitions are allowed to be identical!

// The same definition means that the subject has the same sequence of language symbols # define A 2*3 # define A 2*3 // The two are equivalent and both are the subject of the three language symbols

The following definitions are considered different by ansi c.

# Define A 2*3 // only the subject with one language symbol. You can use the # undef command to redefine the macro.

In # defineUsing the parameter -- class FUNCTION macro

Class FUNCTION macro: The shape and function are similar to those of a function. You can use parameters and enclose them. Generally, parentheses must be added to the body parameters to avoid text replacement errors.

# Define SQRT (x) x * xint main (void) {int x = 2; printf ("% d \ n", SQRT (2 )); // 4 printf ("% d \ n", SQRT (4); // 16 printf ("% d \ n", SQRT (2 + x )); // 8 printf ("% d \ n", SQRT (2 + 2); // No print 16, but 8 = 2 + 2*2 + 2 // must be the main one, class function macros are also text replacement, you can add parentheses to the subject to constrain associativity // avoid using the increment and Decrement Operators printf ("% d \ n", SQRT (++ x) in the macro )); // 16, ++ x * ++ x, x two increments, one before multiplication, one after printf ("% d \ n", x ); // 4 // different compilers handle different things, 4*4 = 16. Here, x is auto-incrementing to 4 twice and then multiplied. Some compilers are different, so macros do not use incremental reduction system ("pause"); return 0 ;}

# In the macro #And ##Operator

# Include <stdio. h> # include <stdlib. h> # define PSQR (X) printf ("x square = % d \ n", (X) * (X); // pay attention to this; is the semicolon of the printf statement int main (void) {PSQR (3); system ("pause"); return 0 ;}

The result is the square of x = 9.

Note that x in the quotation marks of printf is considered as a common text, rather than a language symbol that can be replaced! You can use the # Operator for preprocessing in the replacement part of the class FUNCTION macro, so that normal text is converted to a language symbol that can be replaced! If x is a macro parameter, # x can convert the parameter name to the corresponding string for processing, and output 3 instead of X! (This process is also called string-based ).

# Define PSQR (X) printf (# X "square = % d \ n", (X) * (X); int main (void) {PSQR (3); system ("pause"); return 0 ;}

3 square = 9

Call macros and use "3" instead of # X. Standard C links these strings to generate the final result.

# It can only be used for class function macros, while # operators can be used for class function macros or Class Object macros.

# You can combine two language symbols into a single language symbol!

# Define XNAME (n) X # n // in the class FUNCTION macro, n is a macro parameter (a language symbol), X is a language symbol, plus ##, the two are changed to a language symbol # define PRINT_XN (n) printf ("x" # n "= % d \ n", X # n); // note that X must be capitalized here, because X in XNAME has an uppercase int main (void) {int XNAME (1) = 14; // equivalent to int x1 = 14; PRINT_XN (1); system ("pause "); return 0 ;}

Print x1 = 14

Variable macro

A function can receive fixed parameters or variable parameters (printf function ). The same is true for macros.

Note: Variable, stringized is the word of c, but fixed functions, fixed macros, and unchanged macros are not C words.

Implementation idea: the last parameter in the macro-defined parameter list is the ellipsis... (Three points), the predefined macro _ VA_ARGS _ can be used in the replaced part, indicating the meaning of the ellipsis.

# Include <stdio. h> # include <stdlib. h> # include <math. h> # define PR (X ,...) printf ("variable macro:" # X _ VA_ARGS _); // note that there are two underscores _ int main (void) {double x = 48; double y; y = sqrt (x); // The first parameter corresponds to X of the macro. If the value is 1, # X becomes 1. After the macro is expanded, it becomes: // printf ("variable macro: "" 1 "" x = % g \ n ", x); // then the three strings are connected to: // printf (" variable macro: 1x = % g \ n ", x); // finally point to the output PR (1," x = % g \ n ", x ); // _ VA_ARGS _ tells the ellipsis what it represents // Note: The ellipsis can only represent the last parameter !!! System ("pause"); return 0 ;}

Macros exchange space for time, and functions exchange time for space. For example, if 100 macros are used, the Code represented by 100 macros will be inserted in the program, which wastes space, but the speed is fast. When a function is used for 100 times, the program only copies one function to save space. However, every time a function is called, The stack must be pushed out of the stack and the call point should be returned, which is a waste of time!

Functions that can be processed by simple functions, generally using macros or inline functions.

The macro name cannot contain spaces and must be capitalized (it is customary to remind the programmer that this is a macro). The Non-Detection type of the macro is simply a replacement of text. The advantage is that the int or double type can be used.

Enclose every parameter in the class FUNCTION macro with parentheses. It is recommended that you avoid errors caused by logical confusion caused by text replacement.

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.