Usage of "#" and "# #" in a macro, general usage
We use # To change the macro parameter to a string, with # #把两个宏参数贴合在一起. 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
", CONS (2,3)); 2E3 output: Watts return 0; }
Second, when the macro parameter is another macro
The local macro parameters that need to be noted for ' # ' or ' # # ' in the macro definition are not expanded.
1, not ' # ' and ' # # ' situation #define TOW (2) #define MUL (a,b) (a*b)
printf ("%d*%d=%d
", TOW, TOW, MUL (Tow,tow)); This line of macros is expanded to: printf ("%d*%d=%d", (2), (2), ((2) * (2));
The parameter tow in the MUL will be expanded to (2).
2, when there is ' # ' or ' # # ' #define A (2) #define STR (s) #s #define CONS (a# #e # #b)
printf ("Int max:%s")
", STR (Int_max)); Int_max #include This line is expanded to: printf ("int MAX:%s", "Int_max");
printf ("%s", CONS (A, a)); Compile error This line is: printf ("%s", int (AeA));
Int_max and A are not going to be expanded, but the solution to this problem is simple. Add one more layer of intermediate conversion macros. The idea of adding this macro is to expand all of the macro parameters in this layer, and then the macro in the conversion macro (_STR) 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 macros
printf ("Int max:%s", STR (Int_max)); The maximum value of the Int_max,int type, for a variable #include output is: int max:0x7fffffff STR (int_max)--> _str (0x7fffffff) and then converted to a string;
printf ("%d
", CONS (A, a)); Output is: CONS (A, a)--> _cons ((2), (2))--> Int ((2) E (2))
Third, ' # ' and ' # # ' Some applications Special 1, merge anonymous variable name
#define ___ANONYMOUS1 (Type, var, line) type var# #line #define __ANONYMOUS0 (type, line) ___anonymous1 (type, _anonymou s, line) #define ANONYMOUS (type) __anonymous0 (type, __line__)
Example: ANONYMOUS (static int); namely: static int _anonymous70; 70 indicates the line number; first layer: ANONYMOUS (static int); --> __anonymous0 (static int, __line__); Second layer:--> ___anonymous1 (static int, _anonymous, 70); Third layer:--> static int _anonymous70; That is, each time only the current layer of the macro can be solved, so __line__ in the second layer can be untied;
2. Filling structure
#define FILL (a) {A, #a}
Enum Idd{open, close}; typedef struct msg{ idd ID;
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.