Macro extension in C

This macro

#define f(x) x x
     f (1
     #undef f
     #define f 2
     f)

Expands to this 1 2 1 2in accordance with this link .

Actually, Ive checked with help Xcode Product > Perform Action > Preprocess, but what steps does the preprocessor follow when expanding this macro?

+4
source share
2 answers

Initial situation:

f (1
#undef f
#define f 2
f)

If we link to the link you provided, the macro is pre-processed in 2 stages:

Step 1: preliminary extension of the argument

If the macro overrides this macro, then the new definition takes effect during the preliminary extension of the argument

f, - , 2:

f(1 f) -> f (1 2)

2:

- f :

f(1 2) -> 1 2 1 2



:

#define f(x) x x  
#define g 2
f(1 g)
+3

, , , , #define, .

0

#define f(x) (x) [x]

f (1
#undef f
#define f 2
f)

// , C-. , , , . Product > Action > Preprocess as Ive, .

, , , , x , ( , "1" ), 2 x, , () [] :

1

(1
#undef f
#define f 2
f) [1
    #undef f
    #define f 2
    f]

#undef f, , f 2

2

(1 f) [1 f]

f , , 2

3

(1 2) [1 2]

. 1 2 1 2 .

0

All Articles