Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > macros with double pound signs (##)

Reply
Thread Tools

macros with double pound signs (##)

 
 
davoti@gmail.com
Guest
Posts: n/a
 
      08-28-2007
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

 
Reply With Quote
 
 
 
 
Pietro Cerutti
Guest
Posts: n/a
 
      08-28-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
 
 
 
davoti@gmail.com
Guest
Posts: n/a
 
      08-28-2007
On Aug 28, 10:13 am, Pietro Cerutti <(E-Mail Removed)> wrote:
> (E-Mail Removed) 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



 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      08-28-2007
Pietro Cerutti <(E-Mail Removed)> writes:

> 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
 
Reply With Quote
 
Pietro Cerutti
Guest
Posts: n/a
 
      08-28-2007
Ben Pfaff wrote:
> Pietro Cerutti <(E-Mail Removed)> writes:
>
>> 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
 
Reply With Quote
 
Pietro Cerutti
Guest
Posts: n/a
 
      08-28-2007
(E-Mail Removed) wrote:
> On Aug 28, 10:13 am, Pietro Cerutti <(E-Mail Removed)> wrote:
>> (E-Mail Removed) 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
 
Reply With Quote
 
karthikbalaguru
Guest
Posts: n/a
 
      08-28-2007
On Aug 28, 10:02 pm, (E-Mail Removed) 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

 
Reply With Quote
 
Justin Spahr-Summers
Guest
Posts: n/a
 
      08-28-2007
On Aug 28, 12:58 pm, Pietro Cerutti <(E-Mail Removed)> wrote:
> > #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.

 
Reply With Quote
 
user923005
Guest
Posts: n/a
 
      08-29-2007
On Aug 28, 10:02 am, (E-Mail Removed) 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"

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
WriteLine with <% %> and double quote signs vunet.us@gmail.com ASP General 6 05-05-2007 11:28 AM
Web deployment projects and British pound signs Mark Rae ASP .Net 0 03-14-2006 08:11 PM
cannot convert parameter from 'double (double)' to 'double (__cdecl *)(double)' error Sydex C++ 12 02-17-2005 06:30 PM
Pound signs on keyboard? Lisa Taylor Computer Support 14 03-05-2004 12:25 AM
Explanation of macros; Haskell macros mike420@ziplip.com Python 80 11-07-2003 02:22 AM



Advertisments