所以我知道 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/