C++/C macro definition (define) the Meaning of # # # in macro concatenation __c++

Source: Internet
Author: User

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;


#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;




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;




return 1;


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.