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

来源:互联网 发布:七灯军团戒指淘宝 编辑:程序博客网 时间:2024/05/18 01:07

一、C语言运算符优先级

PrecedenceOperatorDescriptionAssociativity1::Scope resolutionLeft-to-right2++  --Suffix/postfix increment and decrement()Function call[]Array subscripting.Element selection by reference−>Element selection through pointer3++  --Prefix increment and decrementRight-to-left+  Unary plus and minus!  ~Logical NOT and bitwise NOT(type)Type cast*Indirection (dereference)&Address-ofsizeofSize-ofnew,new[]Dynamic memory allocationdelete,delete[]Dynamic memory deallocation4.*   ->*Pointer to memberLeft-to-right5*   /   %Multiplication, division, and remainder6+   Addition and subtraction7<<   >>Bitwise left shift and right shift8<  <=For relational operators < and ≤ respectively>  >=For relational operators > and ≥ respectively9==   !=For relational = and ≠ respectively10&Bitwise AND11^Bitwise XOR (exclusive or)12|Bitwise OR (inclusive or)13&&Logical AND14||Logical OR15?:Ternary conditionalRight-to-Left16=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 OR17throwThrow operator (for exceptions)18,CommaLeft-to-right


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


二、注释

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

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

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

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

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

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


三、贪心法

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

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

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

原创粉丝点击