Usage of "#" and "#" in macro

Source: Internet
Author: User

I. general usage 
We use # to convert the macro parameter into a string, and # to combine the two macro parameters.
Usage:
# Include <cstdio>
# Include <climits>
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
", 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
", Tow, tow, MUL (tow, tow ));
The Macros in this line will be expanded:
Printf ("% d * % d = % d
", (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
", STR (int_max); // int_max # include <climits>
This row is expanded:
Printf ("int MAX: % s
"," Int_max ");

Printf ("% s
", Cons (A, A); // compile Error
This line is:
Printf ("% s
", 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
", STR (int_max); // The maximum value of int_max, int type, is a variable # include <climits>
Output: int MAX: 0x7fffffff
STR (int_max) --> _ STR (0x7fffffff) and then convert it to a string;

Printf ("% d
", 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];

 

======================================

 

Source:

/********************************** <Br/> * <br/> * # convert macro parameters into a string <br/> * # combine two macro parameters <br/> * Author: sunboy_2050 <br/> * Date: <br/> * <br/> ****************************** * ***/<br/> # include <iostream> <br/> # include <climits> <br/> using namespace STD; <br/> # define STR (s) # S <br/> # define cons (a, B) int (A ## e ## B) <br/> # define tow (2) <br/> # define MUL (a, B) (a * B) <br/> # define a 2 <br/> # DEFINE _ STR (s) # S <br/> # define str2 (s) _ STR (s) // convert the macro <br/> # DEFINE _ cons (a, B) int (A # e # B) <br/> # define cons2 (A, B) _ cons (a, B) // conversion macro <br/> # DEFINE _ anonymous1 (type, VAR, line) type var # Line <br/> # DEFINE _ anonymous0 (type, line) _ anonymous1 (type, _ anonymous, line) <br/> # define anonymous (type) _ anonymous0 (type, _ line _) <br/> # define fill (a) {A, # A} <br/> # DEFINE _ type_buf_size (type) sizeof # type <br/> # define type_buf_size (type) _ type_buf_size (type) </P> <p> int main () <br/>{< br/> cout <(STR (ABCD) <Endl; // output "ABCD" <br/> cout <(STR ("ABCD") <Endl; // output "" ABCD "" <br/> cout <cons (2, 3) <Endl; // output 2e3 = 2*10 ^ 3 = 2000 <br/> cout <Endl; <br/> cout <tow <"*" <tow <"=" <MUL (tow, tow) <Endl; // (2) * (2) = (2) * (2) = 4 </P> <p> cout <Endl; <br/> cout <"str2 (int_max): "<str2 (int_max) <Endl; // str2 (int_max) --> _ STR (0x7fffffff) --> _ STR (2147483647) <br/> cout <"cons2 (A, A):" <cons2 (A, A) <Endl; // cons2 (A,) --> _ cons (2, 2) --> int (2e2) --> 2*10 ^ 2 --> 200 </P> <p> cout <Endl; <br/> // cout <anonymous (static INT) <Endl; <br/> cout <Endl; <br/> Enum IDD {open, close }; <br/> typedef struct _ MSG {<br/> idd id; <br/> const char * MSG; <br/>} MSG; <br/> MSG _ MSG [] = {fill (open), fill (close)}; // {fill (open), fill (close )} -- >{{ open, "open" },{ close, "close" }}< br/> cout <_ MSG <Endl; <br/> cout <Endl; <br/> char Buf [type_buf_size (int_max)]; <br/> cout <type_buf_size (int_max) <Endl; </P> <p> return 0; <br/>}< br/>

 

Result:

[Work@db-testing-com06-vm3.db01.baidu.com C ++] $ g ++-O define1 define1.c

[Work@db-testing-com06-vm3.db01.baidu.com C ++] $./define1

ABCD

"ABCD"

2000

 

2*2 = 4

 

Str2 (int_max): 2147483647

Cons2 (A, A): 200

 

 

0x7fbffff750

 

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.