C Traps:优先级常见错误

逻辑与关系运算符
if (flags & FLAG != 0) {...}

这类错误以前也犯过,因为!=的优先级比&要高所以实际上是这样的

if (flags & (FLAG != 0))

可以把&、|也作为一种逻辑操作,而逻辑操作符的优先级总是比比较操作符、运算操作符来得低。可以这样想,首先进行了一些运算,然后才比较他们的值,通过比较有了布尔值,最后就进行逻辑与、或操作了。

 位移与算术运算符

上面的错误可能会在不清楚的情况下使用括号,但是下面的情况则很难会觉察到,记得好几次都是莫名其妙的位移操作出现过问题。

res = hi << 4 + low

实际上相当于:

res = hi << (4 + low)

这种编码错误个人感觉非常难以察觉,尤其在对优先级生疏的情况下。类似的有

res = low + hi << 4

相当于

res = (low + hi) << 4
赋值与比较运算符
while ((c=getc(in) != EOF) {...}

内层的括号是不可缺少的,因为赋值运算优先级低于比较运算符。

虽然有时码农编码是重复劳动,但是码农的基本功还是需要努力修炼的。

给出一个C++的优先级表

PrecedenceOperatorDescriptionAssociativity
1::Scope resolutionLeft-to-right
2++   --Suffix/postfix increment and decrement
()Function call
[]Array subscripting
.Element selection by reference
->Element selection through pointer
3++   --Prefix increment and decrementRight-to-left
+   Unary plus and minus
!   ~Logical NOT and bitwise NOT
(type)Type cast
*Indirection (dereference)
&Address-of
sizeofSize-of
newnew[]Dynamic memory allocation
deletedelete[]Dynamic memory deallocation
4.*   ->*Pointer to memberLeft-to-right
5*   /   %Multiplication, division, and remainder
6+   Addition and subtraction
7<<   >>Bitwise left shift and right shift
8<   <=For relational operators < and ≤ respectively
>   >=For relational operators > and ≥ respectively
9==   !=For relational = and ≠ respectively
10&Bitwise AND
11^Bitwise XOR (exclusive or)
12|Bitwise OR (inclusive or)
13&&Logical AND
14||Logical OR
15?:Ternary conditionalRight-to-left
=Direct assignment (provided by default for C++ classes)
+=   −=Assignment by sum and difference
*=   /=   %=Assignment by product, quotient, and remainder
<<=   >>=Assignment by bitwise left shift and right shift
&=   ^=   |=Assignment by bitwise AND, XOR, and OR
16throwThrow operator (for exceptions)
17,CommaLeft-to-right

记忆:

1. 获得要进行操作的数(()函数返回值或括号表达式值,[]数组下表,. ->成员引用,从左向右结合:a->b->c->d合法)

2. 进行单目操作(自增,自减,取地址,解引用,sizeof等,从右向左结合:*p++等同于*(p++))

3. 进行双目操作(乘除模,加减,位移,关系,位逻辑,逻辑,逻辑运算优先级低于其他关系算术运算,或低于与,从左向右结合)

4. 赋值(从右向左结合:a=b=c)

5. 逗号表达式(从左向右结合)

() 函数调用、括号肯定可以记住

参考:

  1. C traps and pitfalls

  2. http://en.cppreference.com/w/cpp/language/operator_precedence

转载于:https://www.cnblogs.com/lailailai/p/3660536.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值