Why doesn't a+++++b work?

前端 未结 9 1158
悲&欢浪女
悲&欢浪女 2020-11-22 06:26
int main ()
{
   int a = 5,b = 2;
   printf(\"%d\",a+++++b);
   return 0;
}

This code gives the following error:

error: lval

9条回答
  •  南方客
    南方客 (楼主)
    2020-11-22 07:14

    Because it causes undefined behaviour.

    Which one is it?

    c = (a++)++ + b
    c = (a) + ++(++b)
    c = (a++) + (++b)
    

    Yeah, neither you nor the compiler know it.

    EDIT:

    The real reason is the one as said by the others:

    It gets interpreted as (a++)++ + b.

    but post increment requires a lvalue (which is a variable with a name) but (a++) returns a rvalue which cannot be incremented thus leading to the error message you get.

    Thx to the others to pointing this out.

提交回复
热议问题