# Secrets you don't know in define

Source: Internet
Author: User

Usage of "#" and "#" in macro
I. general usage
We use # to convert the macro parameter into a string, and # to combine the two macro parameters.
Usage:
# Include
# Include
Using namespace STD;

# Define STR (s) # s
# Define cons (a, B) int (A ## e ## B)

Int main ()
{
Printf (STR (vck); // output string "vck"
Printf ("% d/N", cons (2000); // 2e3 output:
Return 0;
}

2. When the macro parameter is another macro
It should be noted that all macro parameters that use '#' or '#' in macro definition will not be expanded.

1. Non-'#' and '#'
# Define tow (2)
# Define MUL (a, B) (a * B)

Printf ("% d * % d = % d/N", tow, tow, MUL (tow, tow ));
The Macros in this line will be expanded:
Printf ("% d * % d = % d/N", (2), (2), (2) * (2 )));
The tow parameter in Mul is expanded to (2 ).

2. When '#' or '#' is available
# Define a (2)
# Define STR (s) # s
# Define cons (a, B) int (A ## e ## B)

Printf ("int MAX: % s/n", STR (int_max); // int_max # include
This row is expanded:
Printf ("int MAX: % s/n", "int_max ");

Printf ("% s/n", cons (A, A); // compile Error
This line is:
Printf ("% s/n", INT (AEA ));

Int_max and a will not be expanded. However, the solution to this problem is simple. add another layer of intermediate conversion macro.
The purpose of adding this macro layer is to expand all macro parameters in this layer, so that the macro (_ Str) in the conversion macro can get the correct macro parameters.

# Define a (2)
# DEFINE _ STR (s) # s
# Define STR (s) _ STR (s) // convert macro
# DEFINE _ cons (a, B) int (A ## e ## B)
# Define cons (a, B) _ cons (a, B) // convert a macro

Printf ("int MAX: % s/n", STR (int_max); // The maximum value of int_max, int type, is a variable # include
Output: int MAX: 0x7fffffff
STR (int_max) --> _ STR (0x7fffffff) and then convert it to a string;

Printf ("% d/N", cons (A, ));
Output: 200
Cons (a, a) --> _ cons (2), (2) --> int (2) E (2 ))

Iii. Special Application Cases of '#' and '#'
1. Merge anonymous variable names
# Define ___ anonymous1 (type, VAR, line) type var # Line
# DEFINE _ anonymous0 (type, line) ___ anonymous1 (type, _ anonymous, line)
# Define anonymous (type) _ anonymous0 (type, _ line __)
For example, anonymous (static INT); that is, static int _ anonymous70; 70 indicates the row number;
First layer: anonymous (static INT); --> _ anonymous0 (static int, _ line __);
Layer 2: --> ___ anonymous1 (static int, _ anonymous, 70 );
Layer 3: --> static int _ anonymous70;
That is, the macro of the current layer can only be unlocked at each time, so _ line _ can be unlocked at the second layer;

2. fill Structure
# Define fill (a) {A, #}

Enum IDD {open, close };
Typedef struct MSG {
Idd id;
Const char * MSG;
} MSG;

MSG _ MSG [] = {fill (open), fill (close )};
Equivalent:
MSG _ MSG [] = {open, "open "},
{Close, "close "}};

3. Record File Name
# DEFINE _ get_file_name (f) # F
# Define get_file_name (f) _ get_file_name (f)
Static char file_name [] = get_file_name (_ file __);

4. Obtain the buffer size of the string corresponding to the value type.
# DEFINE _ type_buf_size (type) sizeof # type
# Define type_buf_size (type) _ type_buf_size (type)
Char Buf [type_buf_size (int_max)];
--> Char Buf [_ type_buf_size (0x7fffffff)];
--> Char Buf [sizeof "0x7fffffff"];
This is equivalent:
Char Buf [11]

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.