Operator precedence and evaluation order

前端 未结 5 2181
梦如初夏
梦如初夏 2021-02-09 05:37

I can\'t understand output of this program:

#include
using namespace std;
int main()
{
    int x = 1 , y = 1, z = 1;
    cout << ( ++x || +         


        
5条回答
  •  野性不改
    2021-02-09 06:15

    Let's put the excess parantheses in:

    ( ++x || (++y && ++z ))

    Then it's easy to see that (++y && ++z ) will only be evaluated if ++x is 0. So you can see that irrespective of operator precedence, the short-circutting nature of || means that the right hand side is only evaluated if the left hand side is 0.

    (If the right hand side is evaluted, then note that ++z will only be evaluated if ++y is not 0.)

提交回复
热议问题