473,698 Members | 2,080 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

token pasting help (##)

I am porting some old code from Digital Unix to Linux using token
pasting, which is failing to compile (code simplified):

#define DEBUG(strg,v) printf("debugou tput: "##strg##". \n", v);
main()
{
int x = 5;

DEBUG("variable x is %d",x);
}

Compiler gives the following output:
b.c:6:1: pasting ""debugoutp ut: "" and ""variable x is %d"" does not
give a valid preprocessing token
b.c:6:1: pasting ""variable x is %d"" and "".\n"" does not give a
valid preprocessing token

I am trying to understand the concept of "valid preprocessing token",
but also would like to know how I can achieve what the code tries to
do

Mark

Jun 22 '07 #1
2 5731
ma**********@th ales-is.com wrote:
I am porting some old code from Digital Unix to Linux using token
pasting, which is failing to compile (code simplified):

#define DEBUG(strg,v) printf("debugou tput: "##strg##". \n", v);
main()
{
int x = 5;

DEBUG("variable x is %d",x);
}

Compiler gives the following output:
b.c:6:1: pasting ""debugoutp ut: "" and ""variable x is %d"" does not
give a valid preprocessing token
b.c:6:1: pasting ""variable x is %d"" and "".\n"" does not give a
valid preprocessing token
But notice how nicely the very similar program below behaves. There may
be a lesson here:

#include <stdio.h>

#define DEBUG(strg,v) printf("debugou tput: " strg ".\n", v);

int main(void)
{
int x = 5;

DEBUG("variable x is %d", x);
return 0;
}
[output]
debugoutput: variable x is 5.
Jun 22 '07 #2
ma**********@th ales-is.com writes:
I am porting some old code from Digital Unix to Linux using token
pasting, which is failing to compile (code simplified):

#define DEBUG(strg,v) printf("debugou tput: "##strg##". \n", v);
main()
{
int x = 5;

DEBUG("variable x is %d",x);
}

Compiler gives the following output:
b.c:6:1: pasting ""debugoutp ut: "" and ""variable x is %d"" does not
give a valid preprocessing token
b.c:6:1: pasting ""variable x is %d"" and "".\n"" does not give a
valid preprocessing token

I am trying to understand the concept of "valid preprocessing token",
but also would like to know how I can achieve what the code tries to
do
Token-pasting joins two tokens together; the result must be a single
valid token. If you join the tokens
"foo"
and
"bar"
you get
"foo""bar"
which is not valid as a single token (though it is valid as two
tokens, two consecutive string literals). Apparently the old compiler
on Digital Unix didn't enforce the modern rules.

But you don't *need* to use token-pasting here, since adjacent string
literals are merged anyway. (That's been true since the 1989 ANSI C
standard, so it's likely that will work under Digital Unix as well,
since token-pasting was also introduced by the 1989 ANSI C standard.)

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 22 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2478
by: Cronus | last post by:
Hi the following code the g++ (g++ (GCC) 3.3.3 (Debian 20040422)) emits the error message that pasting of :: and hello is no valid preprocessor token. The g++ 2.95.3 accepts the code. I know that handling of the ## preprocessing operator has changed. How I have to change the macro definiton of A(P)? NO changes at macro calling should be necessary!
3
22942
by: Frodo Baggins | last post by:
Hi All, I have a piece of code (not written by me) that is failing on compile with the error: pasting "xdr_ndmp_connect_open_request" and "," does not give a valid preprocessing token The relevant line from the header is: #define XDR_AND_SIZE(func) (bool_t(*)(XDR*, ...))xdr_##func##,sizeof(func)
5
8643
by: Chris | last post by:
Hi all We have a strange problem with macros: #define SQLST_MAP_IND_O(Tbl,Fld) i##Tbl##_O=i##Tbl##_##Fld; SQLST_MAP_IND_I(RAGREEJ1,FORMFROMTMSTP); => gcc 3.3.4 gives the following error: source.c:173:40: pasting "[" and "RAGREEJ1" does not give a valid
3
3170
by: Wessi | last post by:
Hi, token pasting means, that normally whitespaces and comments are deleted before and after the ## operator. I want the preprocessor to hold an existing whitespace while replacing the argument of a macro. Example: The following define and macro #define ISR_CAT1(NAME) __interrupt void ##NAME(void)
4
3444
by: Nelson Hsu | last post by:
Is there anything like the token pasting operator (##) in C#? I'm trying to port some unmanaged C++ code into C# and I'm not sure how to mimic this behavior.
7
2641
by: Mark Odell | last post by:
I'm running two different compilers against some hairy macros and one, gcc, doesn't like my token pasting result so much. It says, " "." and "foo" does not give a valid preprocessing token ". Some further reading suggests that since .foo is not a valid preproc. token that I can't really do what I want, portably. The macro looks like this: #define DMA_CHECK_SR_BIT(reg, REG, ch) do \ { ...
13
2932
by: Henry Townsend | last post by:
I hope this is on-topic in c.l.c - it's about the C preprocessor more than the language per se, more generally about the K&R behavior, and most specifically about the Sun cpp which is why I've cross posted there. The test case below is taken from an Imake setup (yes, old, I know). There's a Concat() macro which uses old-style /**/ for token pasting (we are not allowed to assume ANSI so ## isn't allowed). I'm trying to paste AND expand...
3
2434
by: Rudhir | last post by:
Hi Everyone, How can I paste tokens with a white space between them? Because, #define Paste(a,b) (a ## b) doesn't put a white space between the tokens.
1
4225
by: jeff | last post by:
Given: ===Program <=== $ cat macro_delay_expansion.c #define args(a,b) a, b #define myprintf(a, b, c) do { \ printf("%d %d %d\n", a, b, c );\ } while(0)
0
8672
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
8600
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
9156
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9021
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
8892
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5860
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
4614
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3038
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
2
2323
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.