Evaluation of C expression

前端 未结 8 2023
南笙
南笙 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:27

    C does short-circuiting of logical expressions, so evaluation of ++i is enough to figure out that m should be true.

    0 讨论(0)
  • 2020-12-11 18:37

    m = ++i || ++j && ++k;

    Since && has higher precedence than || so the expression is interpreted as ++i || (++j && ++k)

    || is short circuiting and so right hand operand of || operator doesn't get evaluated because ++i returns a non zero value.

    0 讨论(0)
提交回复
热议问题