gpt4 book ai didi

c++ - 后增量运算符行为 w.r.t 逗号运算符?

转载 作者:行者123 更新时间:2023-12-01 13:52:16 25 4
gpt4 key购买 nike

在以下代码中:

int main() {
int i, j;

j = 10;
i = (j++, j+100, 999+j);

cout << i;

return 0;
}

输出为 1010 .

但是不应该是 1009,如 ++应该在使用整个表达式后完成?

最佳答案

逗号运算符是一个序列点:例如,正如在 C++17 标准中所说,

Every value computation and side effect associated with the left expression is sequenced before every value computation and side effect associated with the right expression.



因此, ++的效果运算符保证在 999+j 之前发生被评估。

关于c++ - 后增量运算符行为 w.r.t 逗号运算符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61001093/

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