gpt4 book ai didi

c - 为什么 C 中运算符之间的关联性不同?

转载 作者:行者123 更新时间:2023-12-04 10:32:37 27 4
gpt4 key购买 nike

谈到C中运算符的结合性,我想知道为什么具有相同优先级的运算符之间的结合性会有所不同。例如,后缀自增和后缀自减具有左结合性;而前缀自增和前缀自减具有右结合性。对于所有相同优先级的运算符,仅具有左结合性或右结合性不是很简单吗?

这背后有什么原因吗?

最佳答案

Isn't it simple to have just left or right associativity for all the same precedence operators?

是和it is the case in C . 可能您认为前缀和后缀具有相同的优先级是错误的。后缀的优先级高于前缀!

关于为什么某些运算符具有特定的结合性,还有另一个奇怪的案例需要考虑。来自 Wiki ,

For example, in C, the assignment a = b is an expression that returns a value (namely, b converted to the type of a) with the side effect of setting a to this value. An assignment can be performed in the middle of an expression. (An expression can be made into a statement by following it with a semicolon; i.e. a = b is an expression but a = b; is a statement). The right-associativity of the = operator allows expressions such as a = b = c to be interpreted as a = (b = c), thereby setting both a and b to the value of c. The alternative (a = b) = c does not make sense because a = b is not an lvalue.

关于c - 为什么 C 中运算符之间的关联性不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10087666/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com