MACRO QUERY-2

T

Tagore

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

vippstar

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

Chris M. Thomasson

Tagore said:
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.
 

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

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top