Macros and postincrement

后端 未结 8 1945
庸人自扰
庸人自扰 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:51

    If i'm correct, this is happening:

    with MAX replaced with (a>b...) you have printf("%d %d %d\n",a,b,(a++ > b++ ? a++ : b++ ) );

    First, a++ > b++ is checked and both values increased (a = 4, b = 5) afterwards. Then the second b++ gets active, but because it's postincrement, it's increased after the second value b = 5 is printed.

    Sorry for my bad english, but i hope you understand it?! :D

    Greeings from Germany ;-)

    Ralf

提交回复
热议问题