C语言深度解析笔记2:操作符

一、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
new, new[]Dynamic memory allocation
delete, delete[]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
16=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
17throwThrow operator (for exceptions)
18,CommaLeft-to-right


即顺序是:成员运算符 > 单目运算符 > 乘除运算 > 加减运算 > 移位运算 > 关系运算 > 位运算 > 逻辑运算 > 条件运算 > 赋值运算


二、注释

1、注释与代码同步,修改代码的同时修改相应的注释

2、对于全局变量、常量定义等要有详细注释

3、注释尽量用英文,因为有些编辑器不支持中文

4、数值的单位一定要注释,如时、分、秒、毫秒;千米、米、毫米

5、对于变量的范围一定要注释

6、对于函数的参数,返回值及函数功能要有注释


三、贪心法

C语言有这样一个规则,每一个符号应该包含尽可能多的字符,即所谓的贪心法。例如:

a+++b; <==> (a++) + b;

++i+++i+++i; <==> (++i) + (++i) + (++i);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值