耗时大半天,我是一个没有感情的搬运工。。。☹☹☹
针对下表,一起来找反例吧。
From top to bottom, operators are in descending precedence.
大概来讲:
- (数组下标,引用,自增减,!~)>算数运算>移位运算>关系运算>逻辑运算>条件运算符(?:)>赋值运算>comma
- 任何两个逻辑运算符都具有不同的运算优先级,所有的按位运算符优先级高于顺序运算符。每个‘与’运算符的优先级要高于相应的‘或’运算符。
- ==和!=的优先级低于其他关系运算符,所以可以写:a<b==c<d 用以判断ab的大小顺序是否和cd的大小顺序相同。
sizeof的操作数不能为类型转换:表达式sizeof(int)* p明确地解释为(sizeof(int))* p,但不是sizeof((int)* p)
1) Associativity is only used when there are two or more operators of same precedence.The point to note is associativity doesn’t define the order in which operands of a single operator are evaluated.
// Associativity is not used in the below program. Output is compiler dependent.
int x = 0;
int f1()
{
x = 5;
return x;
}
int f2()
{
x = 10;
return x;
}
int main()
{
int p = f1() + f2();
printf("%d ", x);
return 0;
}
2) There is no chaining of comparison operators in C
int a = 10, b = 20, c = 30
// (c > b > a) is treated as ((c > b) > a), associativity
// is left to right. Therefore the value becomes ((30 > 20) >
// which becomes (1 > 20)
参考:
Operator Precedence and Associativity in C - GeeksforGeekswww.geeksforgeeks.org