Macros and postincrement

后端 未结 8 1942
庸人自扰
庸人自扰 2020-12-02 02:13

Here\'s some more weird macro behavior I was hoping somebody could shed light on:

#define MAX(a,b) (a>b?a:b)

void main(void)
{
  int a = 3, b=4;

  print         


        
8条回答
  •  盖世英雄少女心
    2020-12-02 02:43

    So your expansion gives (adjusted for clarity):

    (a++ > b++) ? a++ : b++
    

    ... so (a++ > b++) is evaluated first, giving one increment each and selecting a branch based on the not-yet-incremented values of a and b. The 'else' expression is chosen, b++, which does the second increment on b, which was already incremented in the test expression. Since it's a post-increment, the value of b before the second increment is given to printf().

提交回复
热议问题