Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   stuck with macro (http://www.velocityreviews.com/forums/t741890-stuck-with-macro.html)

Mark 01-12-2011 07:06 AM

stuck with macro
 
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


Ian Collins 01-12-2011 07:17 AM

Re: stuck with macro
 
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

luser- -droog 01-12-2011 07:26 AM

Re: stuck with macro
 
On Jan 12, 1:06*am, "Mark" <mark_cruzNOTFORS...@hotmail.com> 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>

Mark 01-12-2011 07:31 AM

Re: stuck with macro
 
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

Mark 01-12-2011 07:37 AM

Re: stuck with macro
 
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

Mark 01-12-2011 07:59 AM

Re: stuck with macro
 
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


luser- -droog 01-12-2011 08:08 AM

Re: stuck with macro
 
On Jan 12, 1:26*am, luser- -droog <mijo...@yahoo.com> wrote:
> On Jan 12, 1:06*am, "Mark" <mark_cruzNOTFORS...@hotmail.com> 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>

Ian Collins 01-12-2011 08:22 AM

Re: stuck with macro
 
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


All times are GMT. The time now is 03:20 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.