pls explain this macro

I

Ian Collins

Ravi said:
#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.
 
S

santosh

"]
#define JOIN(s1,s2) printf("%s = %s %s = %s",#s1,s1,#s2,s2)[/QUOTE]

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);
 
R

Richard Heathfield

santosh said:
"]
#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);[/QUOTE]

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

santosh

"]
santosh said:
"]
#define JOIN(s1,s2) printf("%s = %s %s = %s",#s1,s1,#s2,s2)
[/QUOTE][/QUOTE]
[ ... ]
Not quite. It would be:

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

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

Indeed. I'd meant to write:

JOIN(str1, str2);
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top