C语言 对于乘法和除法的混合,是否保证了从左到右的结合性?

siotufzp  于 2023-10-16  发布在  其他
关注(0)|答案(2)|浏览(134)

请注意,这与Operator Precedence.. () and ++Undefined behavior and sequence pointsWhy are these constructs using pre and post-increment undefined behavior?以及这里数百个类似的问题无关

不久:标准是否保证关联性
详细示例:从Wikipedia的关于运算符优先级的文章中,operator*operator/具有相同的优先级,它们是Left-to-right运算符。这是否意味着,* 标准**保证 *,这:

int res = x / y * z / t;

评价为

int res = ( ( x / y ) * z ) / t;

或者它的实现定义?
如果有保证的话,你能报个价吗?
只是出于好奇,我总是在这些情况下写括号。
准备删除问题,如果有这样的一个。

jckbn6z7

jckbn6z71#

从最新的公开可用的draft

5.6乘法运算符[表达式mul]

1乘法运算符 *、/和%从左到右分组。

multiplicative-expression:
pm-expression
multiplicative-expression * pm-expression
multiplicative-expression / pm-expression
multiplicative-expression % pm-expression

所以解析将像这样:

int res = x / y * z / t;
int res = (x / y * z) / t;
int res = ((x / y) * z) / t;
dsekswqp

dsekswqp2#

n3337 5.6/1

*乘法运算符 、/和%从左到右分组。

阅读5标准杆。

相关问题