macros with double pound signs (##)

D

davoti

Hi,

I never truly understand how a macro with ## work in C, for example,
if I define

#define X X##_YZ[2]

what and how does this translated into after compilation?
Can't find similar info. googling, would appreciate some detailed
information.

Thanks in advance.

tom
 
P

Pietro Cerutti

Hi,

I never truly understand how a macro with ## work in C, for example,
if I define

#define X X##_YZ[2]

Not very useful here, actually,

The double pound sign is used for token concatenation:
Example:

#define BUILD_NAME(X,Y) X##.##Y

BUILD_NAME("Big", "Jim")

translates to:

"Big.Jim"
 
D

davoti

I never truly understand how a macro with ## work in C, for example,
if I define
#define X X##_YZ[2]

Not very useful here, actually,

The double pound sign is used for token concatenation:
Example:

#define BUILD_NAME(X,Y) X##.##Y

BUILD_NAME("Big", "Jim")

translates to:

"Big.Jim"
Thanks in advance.


Thanks for the information, Sometimes it is used before the token,
other time behind it, in your example, can I do like below? What's the
difference?

#define BUILD_NAME(X,Y) ##X.##Y

Thanks again

tom
 
B

Ben Pfaff

Pietro Cerutti said:
The double pound sign is used for token concatenation:
Example:

#define BUILD_NAME(X,Y) X##.##Y

BUILD_NAME("Big", "Jim")

translates to:

"Big.Jim"

I'm pretty sure that it doesn't, actually.

If you want to write code with that effect, you can much more
simply write:
#define BUILD_NAME(X,Y) X "." Y
so that BUILD_NAME("Big", "Jim") translates to:
"Big" "." "Jim"
which the compiler will then concatenate into a single string,
with the same effect as "Big.Jim".
 
P

Pietro Cerutti

Ben said:
I'm pretty sure that it doesn't, actually.

If you want to write code with that effect, you can much more
simply write:
#define BUILD_NAME(X,Y) X "." Y
so that BUILD_NAME("Big", "Jim") translates to:
"Big" "." "Jim"
which the compiler will then concatenate into a single string,
with the same effect as "Big.Jim".

You are right. I tried to think about the simplest and smallest example
using token concatenation, and I failed miserably ;-)

Next try:

The token created by concatenating X and Y has to be itself a valid token:

#define BUILD_MSG(X) msg_##X

BUILD_MSG(hello)

would translate to

msg_hello
 
P

Pietro Cerutti

Hi,
I never truly understand how a macro with ## work in C, for example,
if I define
#define X X##_YZ[2]
Not very useful here, actually,

The double pound sign is used for token concatenation:
Example:

#define BUILD_NAME(X,Y) X##.##Y

BUILD_NAME("Big", "Jim")

translates to:

"Big.Jim"
Thanks in advance.


Thanks for the information, Sometimes it is used before the token,
other time behind it, in your example, can I do like below? What's the
difference?

Please read my other post, in reply to Ben Pfaff, for corrections on the
explanation :)

The ## goes on the side you want your token to be pasted:

#define BUILD_NAME(X,Y) ##X.##Y

You have two tokens, X and Y, which you want to concatenate, using a dot
in between
You want the dot to appear AFTER X, and Y appear after the dot
-> X ## . ## Y
 
K

karthikbalaguru

Hi,

I never truly understand how a macro with ## work in C, for example,
if I define

#define X X##_YZ[2]

what and how does this translated into after compilation?
Can't find similar info. googling, would appreciate some detailed
information.

In simple terms - It is for token concatenation / token pasting.
Search using the terms "Token Concatenation in C" or "Token Pasting in
C" or "## in C"

Karthik Balaguru
 
J

Justin Spahr-Summers

You have two tokens, X and Y, which you want to concatenate, using a dot
in between
You want the dot to appear AFTER X, and Y appear after the dot
-> X ## . ## Y

Although, to be precise, if you're using the dot to access a structure
member (e.g. somestruct.value), no concatenation is needed, as it is
made up of three separate tokens. I'm not sure if token concatenation
would work for constructing floating-point numbers, but I imagine it
potentially could.
 
U

user923005

Hi,

I never truly understand how a macro with ## work in C, for example,
if I define

#define X X##_YZ[2]

what and how does this translated into after compilation?
Can't find similar info. googling, would appreciate some detailed
information.

Do a web search for "Token pasting operator"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top