C Language Reference Manual Reading Notes: preprocessing instructions #, #, variable parameters

Source: Internet
Author: User

---------------------------------------------------------------
3.3.8 convert a mark into a string [C Language Reference Manual, corner stone (r54)]
---------------------------------------------------------------
The standard C language provides a mechanism to convert macro parameters (after extension) to string constants. Prior to this, programmers had to exploit many vulnerabilities in the C language Preprocessor to achieve the same results in different ways.
In the standard C language, the # mark in the macro definition is treated as a one-dimensional "stringized" operator, followed by the macro formal parameter name. During macro expansion, replace # With the formal parameter name with the actual parameter contained in the string quotation marks. When a string is generated, each blank sequence in the mark parameter table is replaced with a space character. A backslash is added before any embedded quotation marks and backslash to keep its meaning in the string. The blank characters at the beginning and end of the parameter are ignored, so the empty parameters are extended to the blank string "" (even if there is a blank character between commas ).

For example, consider the Standard C language definition of the test macro.

# Define test (a, B) printf (# A "<" # B "= % d/N", (a) <(B ))
Statement test (0, 0 xFFFF); test ('/N', 10); Expand as follows:
Printf ("0" "<" 0 xFFFF "" = % d/N ", (0) <(0 xFFFF ));
Printf ("'// N'" "<" 10 "= % d/N", ('/N') <(10 ));

---------------------------------------------------------------
3.3.9 mark merging in macro expansion [C Language Reference Manual, corner stone (r54)]
---------------------------------------------------------------
When the mark is merged in the Standard C language to form a new mark, it is controlled by the merge operator # In the macro definition. Before scanning more macros, replace the two marks in the # operator in the macro table into one mark. Therefore, this mark must exist: # cannot be placed at the beginning or end of the replace table. If no valid mark is obtained after merging, the result is undefined.

Example:
# Define temp (I) temp # I
Temp (1) = temp (2 + k) + X;
After preprocessing, it becomes:
Temp1 = temp2 + K + X;

In the above example, When you expand temp () + X, there may be a wonderful situation. Macro definition is valid, but # There is no mark to be combined on the right (unless + is encountered, but this is not what we want ). To solve this problem, we should regard the formal parameter I as a special "null" Mark specially designed for # extension. In this way, the result of the temp () + x extension will be temp + X as we expected.
You cannot splice tags to generate common character names.
Similar to converting macro parameters to strings (see section 3.3.8), programmers can exploit many non-standard C language implementation vulnerabilities to obtain the Merge function. Although the original definition of the C language clearly describes the macro as a mark sequence rather than a character sequence, many language compilers extend and rescan the macro as a character sequence. This is more obvious when the compiler processes annotation statements. The Compiler completely deletes the annotation statements instead of replacing them with spaces. Therefore, some ingenious programs can use this feature.
For example, consider the following example:
# Define Inc ++
# Define tab internal_table
# Define inalb table_of_increments
# Define conc (x, y) x/**/y
Conc (Inc, Tab)

In the standard C language, conc is interpreted as two marks X and Y, separated by spaces (the comments are converted into spaces ). Call conc (Inc, Tab) to expand to two Mark Inc tabs. However, some non-standard C languages implement direct deletion of annotation statements, and then re-scan the mark in the macro, so that conc (Inc, Tab) is extended into a mark inctl;

-----------------------------------------------
Steps: Standard C language extensions possible non-standard extensions
-----------------------------------------------
1 conc (Inc, Tab) conc (Inc, Tab)
2 INC/**/tab inc/**/Tab
3 Inc tab inctl
4 ++ internal_table table_of_increments

Test code:

# Define xvarname (I) x # I
# Define pxvarname (n) printf ("X" # N "= % d/N", X # N)

Int K = 0;
Int B = 0;
Int xvarname (K) = 12;
Pxvarname (k + 1); // XK + 1 = 13
Pxvarname (k); // XK = 12
B = xvarname (k) + 0;
Printf ("B = % d, K = % d/R/N", B, k); // B = 12, K = 0
B = xvarname (K + 10) + 0;
Printf ("B = % d, K = % d/R/N", B, k); // B = 22, K = 0

---------------------------------------------------------------
3.3.10 variable parameter table in macro [C Language Reference Manual, corner stone (r54)]
Notice: Visual C ++ does not support c99 variable macro definition until version 8.0.
---------------------------------------------------------------
# Ifdef debug
# Define myprintf (...) fprintf (stderr, _ va_args __)
# Else
# Define myprintf (...) fprintf (_ va_args __)
# Endif

Specifically:
# Define make_em_str (...) #__ va_args __
The following calls
Make_em_str (A, B, C, D)
Expand to string
"A, B, c, d"

Bytes -------------------------------------------------------------------------------------------------------------------------
Digress:
I hate the credit increase method of beneficiary network, but some of the points there are really good. If you want to, please help me:
Http://www.netyi.net
Bytes --------------------------------------------------------------------------------------------------------------------------

 

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.