开发者

Macros in C.... please give the solution

Suppose I declared a macro name anything, xyz().

Now I am creating another macro xyz1() and reference the 1st macro i.e xyz() in 2nd.

Finally, I'll create another macro xyz2() and reference the 2nd macro in 3rd.

开发者_如何学CNow my question is: is this correct (it's executing without any problem)? Macro xyz() is defined twice. Why its not giving an error? What is the solution?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜