469,330 Members | 1,354 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,330 developers. It's quick & easy.

macros with double pound signs (##)

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

Aug 28 '07 #1
8 31746
da****@gmail.com wrote:
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.

--
Pietro Cerutti

PGP Public Key:
http://gahr.ch/pgp
Aug 28 '07 #2
On Aug 28, 10:13 am, Pietro Cerutti <g...@gahr.chwrote:
dav...@gmail.com wrote:
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.

--
Pietro Cerutti

PGP Public Key:http://gahr.ch/pgp

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

Aug 28 '07 #3
Pietro Cerutti <ga**@gahr.chwrites:
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".
--
"What is appropriate for the master is not appropriate for the novice.
You must understand the Tao before transcending structure."
--The Tao of Programming
Aug 28 '07 #4
Ben Pfaff wrote:
Pietro Cerutti <ga**@gahr.chwrites:
>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".
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

--
Pietro Cerutti

PGP Public Key:
http://gahr.ch/pgp
Aug 28 '07 #5
da****@gmail.com wrote:
On Aug 28, 10:13 am, Pietro Cerutti <g...@gahr.chwrote:
>dav...@gmail.com wrote:
>>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.
--
Pietro Cerutti

PGP Public Key:http://gahr.ch/pgp


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
>
Thanks again


--
Pietro Cerutti

PGP Public Key:
http://gahr.ch/pgp
Aug 28 '07 #6
On Aug 28, 10:02 pm, dav...@gmail.com wrote:
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

Aug 28 '07 #7
On Aug 28, 12:58 pm, Pietro Cerutti <g...@gahr.chwrote:
#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
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.

Aug 28 '07 #8
On Aug 28, 10:02 am, dav...@gmail.com wrote:
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"

Aug 29 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Michael Winter | last post: by
37 posts views Thread by hasadh | last post: by
6 posts views Thread by Anthony | last post: by
1 post views Thread by Mark Rae | last post: by
17 posts views Thread by Digital Puer | last post: by
3 posts views Thread by Floobar | last post: by
80 posts views Thread by pereges | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.