1

Following is the code including Combination of Token pasting and Stringizing as follows:

#define f(a,b) a##b
#define g(a) #a
#define h(a) g(a)

printf("%s\n",h(f(1,2))); 
printf("%s\n",g(f(1,2))); 

The output is:

12

f(1,2)

I do not uderstand the difference in ordering in the two expressions.

Freak
  • 6,786
  • 5
  • 36
  • 54
bugger
  • 169
  • 1
  • 1
  • 5

1 Answers1

1

When you use the # expression in the preprocessor, it takes the incoming argument and makes a string of it.

In your code:

#define f(a,b) a##b
#define g(a) #a
#define h(a) g(a)

printf("%s\n",h(f(1,2))); 
printf("%s\n",g(f(1,2))); 

when you call h(f(1,2)), it translates to g(f(1,2)), which in turn is a macro, so the macro expander parses that, starting from the inside with f(1,2), which is 12, then passes that as g(12), which results in "12".

With g(f(1,2)), f(1,2) is used directly, because there is no expansion of with #a, it just uses whatever is passed in as a.

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • this explanation is better then linked answer, but I have confusion why `g(f(1,2))` starts from inside for `h(f(1,2))` – Grijesh Chauhan Jun 17 '13 at 11:10
  • It's not so much that it starts from the inside, but rather that the C preprocessor expands the macro `f(1,2) before passing it to `g` when calling `h`. When calling `g` directly, it just passes `f(1,2)` to the `g` macro. It's the way it works - I can't really explain it better. – Mats Petersson Jun 17 '13 at 11:22