C/C ++数学运算顺序 [英] C/C++ Math Order of Operation

查看:123
本文介绍了C/C ++数学运算顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我知道C ++具有运算符优先级,并且

So I know that C++ has an Operator Precedence and that

int x = ++i + i++;

是未定义的,因为pre ++和post ++处于同一级别,因此无法确定哪个将首先计算.但是我想知道的是

is undefined because pre++ and post++ are at the same level and thus there is no way to tell which one will get calculated first. But what I was wondering is if

int i = 1/2/3;

未定义.我问的原因是因为有多种方法可以查看(1/2)/3或1/(2/3). 我的猜测是这是未定义的行为,但我想确认一下.

is undefined. The reason I ask is because there are multiple ways to look at that (1/2)/3 OR 1/(2/3). My guess is that it is a undefined behavior but I would like to confirm it.

推荐答案

在您的示例中,编译器可以按喜欢的顺序自由计算"1","2"和"3",然后从左到右应用除法

In your example the compiler is free to evaluate "1" "2" and "3" in any order it likes, and then apply the divisions left to right.

对于i ++ + i ++示例来说是相同的.它可以按任何顺序评估i ++,这就是问题所在.

It's the same for the i++ + i++ example. It can evaluate the i++'s in any order and that's where the problem lies.

不是未定义函数的优先级,而是对其参数进行求值的顺序.

It's not that the function's precedence isn't defined, it's that the order of evaluation of its arguments is.

这篇关于C/C ++数学运算顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆