views:

119

answers:

2

suppose i declared a macro name anything, xyz()

and now i am creating another macro xyz1() and referencing the 1st macro i.e xyz() in 2nd.

finally i'll create another macro xyz2() and referencing 2nd macro in 3rd...

now my question is is this correct(its executing without any problem)..? and macro xyz() is defined twice.... why its not giving error ? what is the solution..?

+2  A: 

No, the first macro wiil only be defined once. When you write

#define Symbol SymbolResolution

the preprocessor will substitute Symbol for SymbolResolution wherever it sees Symbol. If SymbolResolution is a #define, or includes some symbols that have #defines inside the same will happen to them - they will all be replaced. This will happen until there're no symbols that have #defines for them in the whole translation unit.

So you can reference macros from other macros as you wish. You can't however reference macros recursively. You also should be careful with this - this can easily lead to lots of barely readable and very hard to debug code if you misuse macros.

sharptooth
+1  A: 

If you mean macros referencing other macros, It is legal.

When the preprocessor expands a macro name, the macro's expansion replaces the macro invocation, then the expansion is examined for more macros to expand.

#define hello() (12)
#define test() (1+hello())

However a macro calling itself is not legal

A self-referential macro is one whose name appears in its definition. Recall that all macro definitions are rescanned for more macros to replace. If the self-reference were considered a use of the macro, it would produce an infinitely large expansion. To prevent this, the self-reference is not considered a macro call.

z33m
also, you can't define (and have it working) `#define make_macro(x) #define macro_ ## x funcname ## x()` (the embedded define will not be evaluated).
SF.