Connecting Tech Pros Worldwide Forums | Help | Site Map

token pasting help (##)

mark.bergman@thales-is.com
Guest
 
Posts: n/a
#1: Jun 22 '07
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


Martin Ambuhl
Guest
 
Posts: n/a
#2: Jun 22 '07

re: token pasting help (##)


mark.bergman@thales-is.com wrote:
Quote:
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.
Keith Thompson
Guest
 
Posts: n/a
#3: Jun 22 '07

re: token pasting help (##)


mark.bergman@thales-is.com writes:
Quote:
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) kst-u@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"
Closed Thread