Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > stuck with macro

Reply
Thread Tools

stuck with macro

 
 
Mark
Guest
Posts: n/a
 
      01-12-2011
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.

--
Mark

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      01-12-2011
On 01/12/11 08:06 PM, Mark wrote:
> 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.

--
Ian Collins
 
Reply With Quote
 
 
 
 
luser- -droog
Guest
Posts: n/a
 
      01-12-2011
On Jan 12, 1:06*am, "Mark" <(E-Mail Removed)> wrote:
> 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.
>
> --
> Mark


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>
 
Reply With Quote
 
Mark
Guest
Posts: n/a
 
      01-12-2011
pete wrote:
> #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

--
Mark
 
Reply With Quote
 
Mark
Guest
Posts: n/a
 
      01-12-2011
Ian Collins wrote:
>
> How about
>
> #define Str(x, y) #x #y
>
> The two strings will then be concatenated by the preprocessor.


This one looks good and very obvious, thank you!

--
Mark
 
Reply With Quote
 
Mark
Guest
Posts: n/a
 
      01-12-2011
pete wrote:
>> Compiler gives an error message:
>> error: pasting ")" and "Str" does not give a valid preprocessing
>> token

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

--
Mark

 
Reply With Quote
 
luser- -droog
Guest
Posts: n/a
 
      01-12-2011
On Jan 12, 1:26*am, luser- -droog <(E-Mail Removed)> wrote:
> On Jan 12, 1:06*am, "Mark" <(E-Mail Removed)> wrote:
>
>
>
> > 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.

>
> > --
> > Mark

>


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>
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      01-12-2011
On 01/12/11 08:23 PM, pete wrote:
> Mark wrote:
>>
>> 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?

>
> /* 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.

--
Ian Collins
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dedicated Macro or Normal Macro? John Ortt Digital Photography 5 11-22-2005 12:43 PM
Macro lens on a camera with a macro setting??? mitchell.chris@gmail.com Digital Photography 2 09-28-2005 07:55 AM
in S.E. Asia : Canon EOS 300d with 100 macro ED vs. Nikon D70 with Nikon 105 macro ? J. Cod Digital Photography 0 09-29-2004 05:46 AM
#define macro to enclose an older macro with strings Dead RAM C++ 20 07-14-2004 10:58 AM
macro name from macro? D Senthil Kumar C Programming 1 09-21-2003 07:02 PM



Advertisments