stuck with macro

M

Mark

Hi

I was reading http://c-faq.com/ansi/stringize.html I'd like to have a
similar functionality for pasting tokens with '##' operator. The page says
that the same approcah should be used for that. Could you give me a hint how
to implement such macros:

#define Str(x, y) x ## y /* two args paste together */
#define XStr(x, y) Str(x, y)

XStr expands first in to Str(), in the second step Str() expands into 'x'
and 'y' merged together.
How will I stringize the result of 'x ## y' ? I think there should be one
more step in here, but where?

Thanks in advance.
 
I

Ian Collins

Hi

I was reading http://c-faq.com/ansi/stringize.html I'd like to have a
similar functionality for pasting tokens with '##' operator. The page
says that the same approcah should be used for that. Could you give me a
hint how to implement such macros:

#define Str(x, y) x ## y /* two args paste together */
#define XStr(x, y) Str(x, y)

XStr expands first in to Str(), in the second step Str() expands into
'x' and 'y' merged together.
How will I stringize the result of 'x ## y' ? I think there should be
one more step in here, but where?

How about

#define Str(x, y) #x #y

The two strings will then be concatenated by the preprocessor.
 
L

luser- -droog

Hi

I was readinghttp://c-faq.com/ansi/stringize.html  I'd like to have a
similar functionality for pasting tokens with '##' operator. The page says
that the same approcah should be used for that. Could you give me a hint how
to implement such macros:

#define Str(x, y) x ## y        /* two args paste together */
#define XStr(x, y) Str(x, y)

XStr expands first in to Str(), in the second step Str() expands into 'x'
and 'y' merged together.
How will I stringize the result of 'x ## y' ?  I think there should be one
more step in here, but where?

Thanks in advance.

656(1)01:25 AM:~ 0> cat strpaste.h
#define Str(x) #x
#define SPaste(x, y) Str(x ## y)
SPaste(ooga, booga)
657(1)01:26 AM:~ 0> gcc -E strpaste.h
# 1 "strpaste.h"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "strpaste.h"


"oogabooga"
658(1)01:26 AM:~ 0>
 
M

Mark

pete said:
#include <stdio.h>

#define str(s) # s

#define XStr(x, y) str(x) ## str(y)

int
main(void)
{
puts(XStr(A, x));
return 0;
}

Compiler gives an error message:
error: pasting ")" and "Str" does not give a valid preprocessing token
 
M

Mark

pete said:
There is no "Str" in the code that I posted.

#include <stdio.h>

#define str(s) # s

#define XStr(x, y) str(x) ## str(y)

int
main(void)
{
puts(XStr(A, x));
return 0;
}

#gcc -ansi -pedantic -W -Wall new.c && ./a.out
new.c:10:1: error: pasting ")" and "str" does not give a valid preprocessing
token
 
L

luser- -droog


I made mine prettier!

684(1)02:03 AM:~ 0> gcc -E - <<\#eof | sed '/^#/d;/^$/d' -
#define s(x) #x
#define p(x,y) s(x##y)
p(this,that)
#eof
"thisthat"
684(1)02:05 AM:~ 0>
 
I

Ian Collins

/* BEGIN new.c */

#include<stdio.h>

#define str(s) # s

#define XStr(x, y) str(x) ## str(y)

The ## is superfluous. So this distils down to my solution.
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top