473,498 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 32766
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
7211
by: Michael Winter | last post by:
In a recent post ("About C error" by Victor, 21 Sep 2003), comments were made about the poster's use of macros. What I would like to know is why they are considered bad? I'm not referring to...
24
22554
by: deko | last post by:
I'm trying to log error messages and sometimes (no telling when or where) the message contains a string with double quotes. Is there a way get the query to insert the string with the double...
37
2993
by: hasadh | last post by:
Hello, probably this may be a simple qn to u all but I need an answer plz. In my software i used macros like OK,TRUE,FALSE,FAILURE . A friend who included this code as a library into his module...
6
6861
by: Anthony | last post by:
When using StreamReader/Writer to process an input text file to an output text file the pound signs (£) simply disappear. Sample code: StreamReader sr = new StreamReader("input.txt");...
1
1422
by: Mark Rae | last post by:
Hi, Suddenly (at least, I think so) when I rebuild a web deployment project in VS.NET 2005, it strips off British pound signs e.g. £10 becomes 10. Has anyone else seen this...? Is there a way...
17
10207
by: Digital Puer | last post by:
I've inherited some code where the coder placed dollar signs in his preprocessor macros. What is the significance of the dollar signs ($) ? Example: #define ALLOCATE(task,pointer) \ { \...
3
1496
by: Floobar | last post by:
Macros sure can be fun -- and profitable. This actually worked -- it might work for any of you guys too. The trick is to make your code look sensible, but be actually very hard to modify without...
80
2841
by: pereges | last post by:
Hello, I have the following structure - typedef struct { double x, y, z; }vector; In certain places, I could avoid triplification of code by using an array instead of x, y, z. For eg:
0
2489
by: RobR2009 | last post by:
I am having trouble with a C# proxy page I am writing which allows me to do cross domain AJAX calls with Javascript. The problem is with certain pages that contain pound signs £ that are not HTML...
0
7126
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7005
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7168
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7210
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6891
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4916
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4595
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3087
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
293
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.