Why does C not have a logical assignment operator?

前端 未结 4 598
面向向阳花
面向向阳花 2020-12-29 21:00

I had the need to code a statement of the form

a = a || expr;

where expr should be evaluated and the result be assigned to <

4条回答
  •  一个人的身影
    2020-12-29 21:24

    I guess the simple answer is that || is a boolean operator: and in C, a "boolean" is 0 or 1. The operands are implicitly converted to booleans (I have not checked that that's what the spec actually says, but it's how C behaves), and the result is a boolean.

    Altering the semantics to support this pattern may well be feasible -- until someone relies on || doing what it's always done.

提交回复
热议问题