一、C语言运算符优先级
Precedence | Operator | Description | Associativity |
---|---|---|---|
1 | :: | Scope resolution | Left-to-right |
2 | ++ -- | Suffix/postfix increment and decrement | |
() | Function call | ||
[] | Array subscripting | ||
. | Element selection by reference | ||
−> | Element selection through pointer | ||
3 | ++ -- | Prefix increment and decrement | Right-to-left |
+ − | Unary plus and minus | ||
! ~ | Logical NOT and bitwise NOT | ||
(type) | Type cast | ||
* | Indirection (dereference) | ||
& | Address-of | ||
sizeof | Size-of | ||
new , new[] | Dynamic memory allocation | ||
delete , delete[] | Dynamic memory deallocation | ||
4 | .* ->* | Pointer to member | Left-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 conditional | Right-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 | ||
17 | throw | Throw operator (for exceptions) | |
18 | , | Comma | Left-to-right |
即顺序是:成员运算符 > 单目运算符 > 乘除运算 > 加减运算 > 移位运算 > 关系运算 > 位运算 > 逻辑运算 > 条件运算 > 赋值运算
二、注释
1、注释与代码同步,修改代码的同时修改相应的注释
2、对于全局变量、常量定义等要有详细注释
3、注释尽量用英文,因为有些编辑器不支持中文
4、数值的单位一定要注释,如时、分、秒、毫秒;千米、米、毫米
5、对于变量的范围一定要注释
6、对于函数的参数,返回值及函数功能要有注释
三、贪心法
C语言有这样一个规则,每一个符号应该包含尽可能多的字符,即所谓的贪心法。例如:
a+++b; <==> (a++) + b;
++i+++i+++i; <==> (++i) + (++i) + (++i);