7

Possible Duplicate:
Why i am not getting the expected output in the following c programme?

i am having a doubt in order of evaluation of macros.like for the following code i am not able to understand the ouput:

#include <stdio.h>
#define f(a,b) a##b
#define g(a) #a
#define h(a) g(a) 
int main()
{
    printf("%s\n",h(f(1,2)));
    printf("%s\n",g(f(1,2)));
    return 0;
}

Output

12
f(1,2)

why not f is getting expanded first before g in the second printf ?

Community
  • 1
  • 1
Amol Sharma
  • 1,521
  • 7
  • 20
  • 40

1 Answers1

0

It's a consequence of how macros are expanded, and has an impact on self-referential macros... This is quite nicely explained in detail in the GNU CPP manual

Ahmed Masud
  • 21,655
  • 3
  • 33
  • 58