0
#define N 1 #define A(N) #N #define S_A A(N) #define B_(N) #N #define B(N) B_(N) #define S_B B(N) #include <stdio.h> int main(void) { puts(S_A); puts(S_B); } 

outputs N and 1 instead of two 1s.

Why is the extra indirection making a difference?

2 Answers 2

2

It works different because the macro expansion of N only happens on the "use" of N. So S_A exands to A(N), which is expanded to #N. In S_B it is expanded to B(N), which is expanded to _B(1), and then #1. Why? Well, how would you do something like:

 #define COMBINE(A, B) A##B #define foo 1 #define bar 2 int COMBINE(foo, bar) = 34; 

so that it generates int foobar = 34;, and not the illegal int 12 = 34;.

Sign up to request clarification or add additional context in comments.

Comments

0

Did you intend to use a ## operator for token pasting?

puts(S_A); //Gets expanded into puts(A(N)) --> puts(#N) 

and

puts(S_B); //Gets expanded into puts(B(N)) --> puts(_B(1)) --> puts(#1) 

Without affecting your macros much, if you intend to print 1,1 both the times, you could Use the macros this way:

#define N "1" //instead of 1 #define A(N) N //instead of #N #define B_(N) N //instead of #N 

Comments