Why the following function is called thrice

后端 未结 6 1273
轻奢々
轻奢々 2021-01-20 10:45

I had tried to debug but no luck I can\'t understand why the second printf() is call increment() thrice but the first one call twice as expected.

#include &l         


        
6条回答
  •  终归单人心
    2021-01-20 11:31

    Use of macros is highly "dangerous": all sort of strange things can happen. For instance, in your case, if you call MAX( f(), g() ) the function which gives you the greatest result gets called twice, the other one gets called just once. Since you are using MAX(x, f()), f gets called twice if and only if it gives a result which is greater than x.

    In particular, the macro expands as

     ( (x) > (increment())? (x):(increment()) )
    

    so that if the condition (testing which requires one evaluation of increment(), increment() gets executed in order to produce the result.

提交回复
热议问题