的优先级大小_C 运算符的优先级与结合性

耗时大半天,我是一个没有感情的搬运工。。。☹☹☹

针对下表,一起来找反例吧。

From top to bottom, operators are in descending precedence.

3a9d321d3c721b5611ec6e5ea551300b.png

大概来讲:

  • (数组下标,引用,自增减,!~)>算数运算>移位运算>关系运算>逻辑运算>条件运算符(?:)>赋值运算>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 - GeeksforGeeks​www.geeksforgeeks.org
abcddf8e23d82f84a47f493005d06415.png
C Operator Precedence​en.cppreference.com
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值