Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > MACRO QUERY-2

Reply
Thread Tools

MACRO QUERY-2

 
 
Tagore
Guest
Posts: n/a
 
      01-08-2009
Please consider program given below;

#include<stdio.h>
#define f(a,b) a##b
#define g(a) #a
#define h(a) g(a)

int main(void)
{
printf("%s\n",h(f(1,2)));
printf("%s\n",g(f(1,2)));
return 0;
}

above program gives following output :
12
f(1,2)


I expected both printf to give same output but why is it showing
different outputs?
 
Reply With Quote
 
 
 
 
vippstar@gmail.com
Guest
Posts: n/a
 
      01-08-2009
On Jan 8, 6:59*pm, Tagore <(E-Mail Removed)> wrote:
> Please consider program given below;
>
> #include<stdio.h>
> #define f(a,b) a##b
> #define g(a) #a
> #define h(a) g(a)
>
> int main(void)
> {
> * * * * *printf("%s\n",h(f(1,2)));
> * * * * *printf("%s\n",g(f(1,2)));
> * * * * *return 0;
>
> }
>
> above program gives following output :
> 12
> f(1,2)
>
> I expected both printf to give same output but why is it showing
> different outputs?


See question 10.20 of the C-FAQ. <http://c-faq.com/>
 
Reply With Quote
 
 
 
 
Chris M. Thomasson
Guest
Posts: n/a
 
      01-08-2009
"Tagore" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Please consider program given below;
>
> #include<stdio.h>
> #define f(a,b) a##b
> #define g(a) #a
> #define h(a) g(a)
>
> int main(void)
> {
> printf("%s\n",h(f(1,2)));
> printf("%s\n",g(f(1,2)));
> return 0;
> }
>
> above program gives following output :
> 12
> f(1,2)
>
>
> I expected both printf to give same output but why is it showing
> different outputs?


`h' has another level of expansion which actually renders the token `f(1,2)'
into `12'. However, `g' does not have that extra level, therefore it has no
chance to render `f(1,2)', so it stays as-is.

 
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