c++ - C/C++ 数学运算顺序

标签 c++ c math operator-precedence

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

int x = ++i + i++;

未定义,因为 pre++ 和 post++ 处于同一级别,因此无法判断哪个先计算。但我想知道的是,如果

int i = 1/2/3;

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

最佳答案

如果您查看 C++ operator precedence and associativity ,您会看到除法运算符是从左到右结合的,这意味着这将被计算为 (1/2)/3,因为:

Operators that are in the same cell (there may be several rows of operators listed in a cell) are evaluated with the same precedence, in the given direction. For example, the expression a=b=c is parsed as a=(b=c), and not as (a=b)=c because of right-to-left associativity.

关于c++ - C/C++ 数学运算顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11296854/

相关文章:

python - 求解非线性方程组和线性不等式的快速方法?

c++ - 如何解决用文字 0 和指针调用重载函数的歧义

c++ - boost::variant 访问者选择了错误的重载

有人可以解释 fork 是如何工作的吗?

c - C 中的 __do_global_dtors_aux

algorithm - 找到给定音调和 987654321 的除法余数

c - 修改给定的数字以找到所需的总和?

c++ - 类中类对象构造函数/初始化失败

c++ - 不同的值取决于 C++ 类型

c - Linux 4.5 GPIO 中断通过 Xilinx Zynq 平台上的 Devicetree