473,386 Members | 1,873 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,386 software developers and data experts.

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("debugoutput: "##strg##".\n", v);
main()
{
int x = 5;

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

Compiler gives the following output:
b.c:6:1: pasting ""debugoutput: "" 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 5713
ma**********@thales-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("debugoutput: "##strg##".\n", v);
main()
{
int x = 5;

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

Compiler gives the following output:
b.c:6:1: pasting ""debugoutput: "" 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("debugoutput: " 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**********@thales-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("debugoutput: "##strg##".\n", v);
main()
{
int x = 5;

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

Compiler gives the following output:
b.c:6:1: pasting ""debugoutput: "" 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_Keith) 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
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...
3
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...
5
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:...
3
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...
4
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
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...
13
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...
3
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
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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...

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.