Macros and postincrement

后端 未结 8 1944
庸人自扰
庸人自扰 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 03:00

    I think the questioner was expecting the output to start:

    3 4 ...
    

    instead of:

    4 6 ...
    

    and this is because the parameters are being evaluated from right to left as they are being pushed onto the stack, that is, the last parameter is being evaluated first and pushed, then the second to last, then the second parameter and finally the first parameter.

    I think (and someone post a comment if it's wrong) that this is defined in the C standard (and C++).

    Update

    The order of evaluation is defined, but it's defined as undefined (thanks Steve). Your compiler just happens to do it this way. I think I got confused between evaluation order and the order the parameters are passed.

提交回复
热议问题