Connecting Tech Pros Worldwide Forums | Help | Site Map

pls explain this macro

Ravi
Guest
 
Posts: n/a
#1: Jul 18 '07
#define JOIN(s1,s2) printf("%s = %s %s = %s",#s1,s1,#s2,s2)

Ian Collins
Guest
 
Posts: n/a
#2: Jul 18 '07

re: pls explain this macro


Ravi wrote:
Quote:
#define JOIN(s1,s2) printf("%s = %s %s = %s",#s1,s1,#s2,s2)
>
What's "pls"? Please put your question in your message body.

Have you tried the macro? Your C book should explain the operation of #
on a macro argument.

--
Ian Collins.
santosh
Guest
 
Posts: n/a
#3: Jul 18 '07

re: pls explain this macro


On Wednesday 18 Jul 2007 1:53 pm, in comp.lang.c, Ravi
<ra.ravi.rav@gmail.comwrote:
Message ID: <1184747027.585196.289450@j4g2000prf.googlegroups. com>
Quote:
#define JOIN(s1,s2) printf("%s = %s %s = %s",#s1,s1,#s2,s2)
The name JOIN is rather misleading. The # preprocessor operator
causes it's accompanying argument to be "stringised", i.e., for it
to enclosed within double quotes before further processing.

So in your case, if you write:

JOIN(str1, str2)

the replacement token would be:

printf("%s = %s %s = %s", "str1", str1, "str2", str2);


Richard Heathfield
Guest
 
Posts: n/a
#4: Jul 18 '07

re: pls explain this macro


santosh said:
Quote:
On Wednesday 18 Jul 2007 1:53 pm, in comp.lang.c, Ravi
<ra.ravi.rav@gmail.comwrote:
Message ID: <1184747027.585196.289450@j4g2000prf.googlegroups. com>
>
Quote:
>#define JOIN(s1,s2) printf("%s = %s %s = %s",#s1,s1,#s2,s2)
>
The name JOIN is rather misleading. The # preprocessor operator
causes it's accompanying argument to be "stringised", i.e., for it
to enclosed within double quotes before further processing.
>
So in your case, if you write:
>
JOIN(str1, str2)
>
the replacement token would be:
>
printf("%s = %s %s = %s", "str1", str1, "str2", str2);
Not quite. It would be:

printf("%s = %s %s = %s","str1",str1,"str2",str2)

The most important difference is, of course, that your semicolon is
spurious.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
santosh
Guest
 
Posts: n/a
#5: Jul 18 '07

re: pls explain this macro


On Wednesday 18 Jul 2007 3:12 pm, in comp.lang.c, Richard Heathfield
<rjh@see.sig.invalidwrote:
Message ID: <6fWdnewKTZl2QgDbRVnygwA@bt.com>
Quote:
santosh said:
>
Quote:
>On Wednesday 18 Jul 2007 1:53 pm, in comp.lang.c, Ravi
><ra.ravi.rav@gmail.comwrote:
>Message ID: <1184747027.585196.289450@j4g2000prf.googlegroups. com>
>>
Quote:
>>#define JOIN(s1,s2) printf("%s = %s %s = %s",#s1,s1,#s2,s2)
[ ... ]
Quote:
Quote:
>So in your case, if you write:
>>
> JOIN(str1, str2)
>>
>the replacement token would be:
>>
> printf("%s = %s %s = %s", "str1", str1, "str2", str2);
>
Not quite. It would be:
>
printf("%s = %s %s = %s","str1",str1,"str2",str2)
Yes!
Quote:
The most important difference is, of course, that your semicolon is
spurious.
Indeed. I'd meant to write:

JOIN(str1, str2);


Closed Thread