token pasting help (##)

M

mark.bergman

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
 
M

Martin Ambuhl

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.
 
K

Keith Thompson

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.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,044
Latest member
RonaldNen

Latest Threads

Top