C language macro definition of#, ##,#@and \ symbols of the definition of C -language macro definition symbols


1,# (Stringizing) Stringing operator

Function: Passing in the definition of macroparameter nameConvert to a pair of dual -quoted numbers and parameter name string. It can only be used in a macro definition with a parameter, and must be placed in front of the parameter name in the macro definition body.


#define example( instr )  printf( “the input string is:\t%s\n”, #instr )

#define example1( instr )  #instr

When using this macro definition:

Example (ABC); // will be launched when compiling: Printf (“The input String is: \ t%s \ n”, “abc”);

string str = example1 (ABC); // will be exhibited: string str = “abc”;

Note, the treatment of the space:

A. Lost the space in front of and behind the parameter name.

, such as: str = example1 (ABC); will be extended to str = “abc”;

B. When there is a space between the parameter names, the compiler will automatically connect each sub -string, and connect with a space between each sub -string to ignore the remaining spaces.

, such as: str = exapme (abc def); will be extended to str = “abc def”;

2, ## (Token-Pasting) symbol connection operator

Function: Convert multiple forms of macro -defined into a realityParameter name

, such as:

           #define exampleNum( n )    num##n


          int num9 = 9;

int Num = Examplenum (9); // will be extended to int num = num9;


A. When the ## is connected on time, the space before and after ## is optional.

, such as: #Define Examplenum (n) num ## N

// equivalent to #Define Examplenum (n) num ## n

B. The actual parameter name after connection must be the actual parameter name or the macro definition of the compiler.

C. If the parameters after ## are also a macro, ## will prevent this macro development.

#include <stdio.h> 
 #include <string.h> 

 #Define Strcpy (A, B) strcpy (a ## _p, #b) 
 int Main () 
     char var1_p [20]; 
     char var2_p [30]; 
     strcpy (var1_p, "aaaa"); 
     strcpy (var2_p, "bbbb"); 
     Strcpy (var1, var2); 
     Strcpy (var2, var1); 
     Printf ("var1 = %s \ n", var1_p); 
     Printf ("var2 = %s \ n", var2_p); 

     // Strcpy (strcpy (var1, var2), var2); 
     // Will it start here: Strcpy (strcpy (var1_p, "var2") _ p, "var2")? the answer is negative: 
     // The result will be: strcpy (strcpy (var1, var2) _p, "var2") 
     // ## Stop the macro exhibition of the parameters! If there is no#and ## in the definition of the macro definition, the macro will be fully expanded 
     // If you open the comment, you will report an error: Implicit Declaration of Function 'Strcpy' 
     Return 0; 


var1 = var2
var2 = var1

3,#@ (Charizing) single -characterized operator

Function: Convert the parameter of the single character into characters, and use a pair of single quotes.

Such as:

#define makechar(x)     #@x

a = makechar( b );

After the expansion, it turned into:

a = ‘b’;

4, \ Renewal operator

When the definition of the definition cannot be fully expressed with a line, you can use “\” to represent the next line of definition.

Note \ Before the space.

macro parameter is another macro

What needs to be noticed is FanThe macro parameters that are useful in the Hong Dingyi is useful in the local macro parameter that will not be expanded again

1, non -‘#’ and ‘##’
#define TOW       (2) 
#define MUL(a,b) (a*b) 
printf(“%d*%d=%d\n”, TOW, TOW, MUL(TOW,TOW)); 
The macro of this line will be unfolded:

The parameters in
printf(“%d*%d=%d\n”, (2), (2), ((2)*(2))); 
Mul will be expanded as (2).
2, when there is ‘#’ or ‘##’
#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 #i nclude 
This line will be unfolded:
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 again,The method of solving this problem is very simple.
plus the purpose of this layer of macro is to expand all the parameters of the macro in this layer
plus the purpose of this layer of macro is to expand all the parameters of the macro in this layer, then the macro (_Str) in the converting macro can get the correct macro ginseng.
#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 macro
Printf (“int Max: %s \ n”, str (int_max)); // int_max, int type, is a variable #i nclude
output is: int Max: 0x7fffffffF
str (int_max) -> _Str (0x7FFFFFFFFF) and then convert to a string;
printf(“%d\n”, CONS(A, A)); 
output to: 200
CONS(A, A)   –>   _CONS((2), (2))   –> int((2)e(2)) 



Random Posts

eureka-client service registration/heartbeat mechanism for source code of source code

Docker install Elasticsearch and Kibana

NCOV data brief analysis (0326)

Laughing Design Mode (Two) ARIC

Brief understand Android Studio