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

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top