473,624 Members | 2,515 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 32784
da****@gmail.co m 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.chwr ote:
dav...@gmail.co m 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.chwr ites:
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.chwr ites:
>The double pound sign is used for token concatenation:
Example:

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

BUILD_NAME("Bi g", "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.co m wrote:
On Aug 28, 10:13 am, Pietro Cerutti <g...@gahr.chwr ote:
>dav...@gmail.c om 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("Bi g", "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.co m 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.chwr ote:
#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.valu e), 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.co m 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
7223
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 their use as 'functions'; I realise the loss of type-safety and the possible evaluation errors that can occur. However, what would be the harm with numeric and text literals? Consider a number that plays a significant role in the implementation of...
24
22604
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 quotes? Do I need to use code to scrub each string and remove or escape the double quotes before using it in a query? The error I get is this: Error Number 3075: Syntax error (missing operator) in query expression '"credit card billed by...
37
3024
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 said that in his code he couldnt use the above words as function names or variable names and got compilation errors. He advised me to use them as enums instead of macros. is he right ??? (in my code the macros are used as return values from fns...
6
6876
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"); StreamWriter sw = new StreamWriter("output.txt"); string Line; char delimiter = {'*'}; while((Line = sr.ReadLine()) != null) { string Fields;
1
1426
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 round it...? Any assistance gratefully received. Mark
17
10256
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) \ { \ long task$; \ if (task == FOO) { \ task$ = ZERO); \
3
1506
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 introducing unexpected side effects, so that anyone hired to replace you will resign, shoot himself, jump off a bridge, or some such within weeks of exposure to your code that they'd have to maintain. At <company name deleteda friend wrote...
80
2905
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
2513
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 encoded in the source that I am trying to extract with the WebRequest and WebResponse objects. The page is using a charset of iso-8859-1 which I think is the problem as my object is using UTF-8. I have created two test pages one using UTF-8 the...
0
8231
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8168
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8614
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6107
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5561
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4075
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4167
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2603
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.