Operator precedence and evaluation order

前端 未结 5 2183
梦如初夏
梦如初夏 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:19

    This expression

     ++x || ++y && ++z
    

    is equivalent to expression

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

    and not like this

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

    If the first operand of the logical OR expression is evaluated to true (as in your example) then the second operand that is ( ++y && ++z ) is not evaluated.

提交回复
热议问题