473,388 Members | 866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

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 32753
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
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
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
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
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
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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...

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.