Evaluation of C expression

前端 未结 8 2064
南笙
南笙 2020-12-11 18:01
int main() {
  int i = -3, j = 2,  k = 0, m;
  m = ++i || ++j && ++k;
  printf(\"%d %d %d %d\\n\", i, j, k, m);
  return 0;
}

i thought tha

8条回答
  •  抹茶落季
    2020-12-11 18:18

    C++ uses lazy evaluation for logical operators.
    If you write a || b, and a is true, b will never evaluate, since the result will be true even if b is false.
    Similarly, a && b will not evaluate b if a is false.

    Since ++i evaluates to a truthy value, none of the other expressions are evaluated.

提交回复
热议问题