Original link: Click to open the link
C++/C macro definition (define) the Meaning of # # #
The # # # in define is commonly used for stitching strings, but in the actual use of the process, there are some subtle differences, we have a few examples to see.
#是字符串化的意思, the # that appears in the macro definition is to turn the arguments that follow in to a string;
eg
#define STRCPY__ (DST, SRC) strcpy (DST, #src)
Strcpy__ (BUFF,ABC) is equivalent to strcpy__ (buff, "abc")
# #是连接符号, connect the parameters together
#define FUN (ARG) my# #arg
Then FUN (ABC)
Equivalent to MYABC
Let's look at a concrete example.
#include <iostream>
using namespace Std;
#define OUTPUT (a) cout<< #A << ":" << (a) <<endl;
int main () {
int a=1,b=2;
OUTPUT (a);
OUTPUT (b);
OUTPUT (A+B);
return 1; }
Get rid of the # number. We got the result and printed the value of the a,b directly, which is in line with the grammatical rules, so the role of # is obvious. #include <iostream>
using namespace Std;
#define OUTPUT (a) cout<<a<< ":" << (a) <<endl;
int main ()
{
int a=1,b=2;
OUTPUT (a);
OUTPUT (b);
OUTPUT (A+B);
return 1;
}