Which operator(s) in C have wrong precedence?

前端 未结 4 2096
面向向阳花
面向向阳花 2021-02-08 07:17

In the \"Introduction\" section of K&R C (2E) there is this paragraph:

C, like any other language, has its blemishes. Some of the operators h

4条回答
  •  醉酒成梦
    2021-02-08 07:47

    Yes, the situation discussed in the message you link to is the primary gripe with the precedence of operators in C.

    Historically, C developed without &&. To perform a logical AND operation, people would use the bitwise AND, so a==b AND c==d would be expressed with a==b & c==d. To facilitate this, == had higher precedence than &. Although && was added to the language later, & was stuck with its precedence below ==.

    In general, people might like to write expressions such as (x&y) == 1 much more often than x & (y==1). So it would be nicer if & had higher precedence than ==. Hence people are dissatisfied with this aspect of C operator precedence.

    This applies generally to &, ^, and | having lower precedence than ==, !=, <, >, <=, and >=.

提交回复
热议问题