Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hashtag symbol in C++ macro expression [duplicate]

Tags:

c++

macros

c++14

In the macro definition below, what does '#' symbol do? What is this syntax (#x) here?

#define print(x) cout<<(#x)<<" : "<<x<<endl;
like image 712
jack7 Avatar asked Oct 18 '25 16:10

jack7


1 Answers

# is the stringify operator. It turns the macro argument x into a string literal.

I can see no reason for the extra parens, #define print(x) cout << #x <<" : " << x << endl; would work just as well. Even better would be #define print(x) cout << #x <<" : " << (x) << endl; because the second use of x might require parens to be parsed correctly.

like image 67
john Avatar answered Oct 21 '25 05:10

john



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!